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 的优点是什么
基础教程推荐
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- c++ STL设置差异 2022-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
