boost::noncopyable 的优点是什么 [英] What are the advantages of boost::noncopyable

查看:26
本文介绍了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:

  1. 它的意图更加明确和描述性.使用私有复制函数是一种比 noncopyable 需要更长的时间才能发现的习惯用法.
  2. 代码更少/打字更少/混乱更少/出错空间更少(最简单的方法是意外提供实现).
  3. 它在类型的元数据中嵌入了正确的含义,类似于 C# 属性.您现在可以编写一个仅接受不可复制对象的函数.
  4. 它可能会在构建过程的早期捕获错误.错误将在编译时而不是链接时出现,以防类本身或类的朋友进行错误复制.
  5. (几乎与 #4 相同)防止类本身或类的朋友调用私有复制方法.
  1. It is more explicit and descriptive in the intent. Using private copy functions is an idiom that takes longer to spot than noncopyable.
  2. It is less code / less typing / less clutter / less room for error (the easiest would be accidentally providing an implementation).
  3. 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.
  4. 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.
  5. (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:

  1. 没有 boost 依赖

这篇关于boost::noncopyable 的优点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆