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 的优点是什么
基础教程推荐
- CString 到 char* 2021-01-01
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 初始化列表*参数*评估顺序 2021-01-01
