What are the advantages of boost::noncopyable(boost::noncopyable 的优点是什么)
问题描述
为了防止复制一个类,你可以很容易地声明一个私有的复制构造函数/赋值运算符.但是你也可以继承boost::noncopyable
.
To prevent copying a class, you can very easily declare a private copy constructor / assignment operators. But you can also inherit boost::noncopyable
.
在这种情况下使用 boost 的优点/缺点是什么?
What are the advantages / disadvantages of using boost in this case?
推荐答案
总结别人的看法:
boost::noncopyable
相对于私有复制方法的优势:
Advantages of boost::noncopyable
over private copy methods:
- 它的意图更加明确和描述性.使用私有复制函数是一种比
noncopyable
需要更长的时间才能发现的习惯用法. - 代码更少/打字更少/混乱更少/出错空间更少(最简单的方法是意外提供实现).
- 它在类型的元数据中嵌入了正确的含义,类似于 C# 属性.您现在可以编写一个仅接受不可复制对象的函数.
- 它可能会在构建过程的早期捕获错误.错误将在编译时而不是链接时出现,以防类本身或类的朋友进行错误复制.
- (几乎与 #4 相同)防止类本身或类的朋友调用私有复制方法.
- It is more explicit and descriptive in the intent. Using private copy functions is an idiom that takes longer to spot than
noncopyable
. - It is less code / less typing / less clutter / less room for error (the easiest would be accidentally providing an implementation).
- It embeds meaning right in the type's metadata, similar to a C# attribute. You can now write a function which accepts only objects which are noncopyable.
- It potentially catches errors earlier in the build process. The error will be presented at compile-time rather than link-time, in the case that the class itself or friends of the class are doing the erroneous copying.
- (almost the same as #4) Prevents the class itself or friends of the class from calling the private copy methods.
私有复制方法相对于 boost::noncopyable
的优势:
Advantages of private copy methods over boost::noncopyable
:
- 没有 boost 依赖
这篇关于boost::noncopyable 的优点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:boost::noncopyable 的优点是什么
基础教程推荐
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07