boost::noncopyable 的优点是什么
为了防止复制类,您可以非常轻松地声明私有复制构造函数/赋值运算符。但您也可以继承boost::noncopyable
。
在这种情况下使用 boost 有哪些优点/缺点?
To prevent copying a class, you can very easily declare a private copy constructor / assignment operators. But you can also inherit boost::noncopyable
.
What are the advantages / disadvantages of using boost in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我没有看到任何文档优势:
vs:
当您添加仅移动类型时,我什至认为文档具有误导性。下面的两个例子是不可复制的,尽管它们是可移动的:
vs:
在多重继承下,甚至可能会有空间损失:
对我来说,这会打印出来:
但是,我相信它有更好的文档:
输出:
我发现它很多声明我的复制操作比推理我是否多次从
boost::non_copyable
派生以及这是否会让我付出代价更容易。特别是如果我不是完整继承层次结构的作者。I see no documentation benefit:
vs:
When you add move-only types, I even see the documentation as misleading. The following two examples are not copyable, though they are movable:
vs:
Under multiple inheritance, there can even be a space penalty:
For me this prints out:
But this, which I believe to have superior documentation:
Outputs:
I find it much easier to declare my copy operations than to reason whether or not I'm deriving from
boost::non_copyable
multiple times and if that is going to cost me. Especially if I'm not the author of the complete inheritance hierarchy.总结一下其他人所说的:
boost::noncopyable相对于私有复制方法的优点:
noncopyable
更难发现的习惯用法。私有复制方法相对于
boost::noncopyable
的优点:Summarizing what others have said:
Advantages of
boost::noncopyable
over private copy methods:noncopyable
.Advantages of private copy methods over
boost::noncopyable
:它使意图明确且清晰,否则就必须查看类的定义,并搜索与复制语义相关的声明,然后查找其所在的访问说明符< em>声明,以确定该类是否不可复制。通过编写需要启用复制语义的代码并查看编译错误来发现它的其他方法。
It makes the intent explicit and clear, otherwise one has to see the definition of the class,and search for the declaration related to copy-semantic, and then look for the access-specifier in which it is declared, in order to determine whether the class is noncopyable or not. Other way to discover it by writing code that requires copy-semantic enabled and see the compilation error.
我不明白为什么其他人似乎没有提到它,但是:
使用
noncopyable
你只需写一次类的名称。如果没有,五重重复:
一个 A 代表“A 类”,两个用于禁用赋值,两个用于禁用复制构造函数。
I can't understand why no one else seem to mention it, but:
With
noncopyable
you write the name of your class just once.Without, fivefold duplication:
One A for 'class A', two to disable the assignment, and two to disable the copy constructor.
引用文档:
“处理这些问题的传统方法是声明一个私有复制构造函数和复制赋值,然后记录为什么这样做。但是从 noncopyable 派生更简单、更清晰,并且不需要额外的文档."
http://www.boost.org/libs/utility/utility.htm#Class_noncopyable
Quoting the documentation:
"The traditional way to deal with these is to declare a private copy constructor and copy assignment, and then document why this is done. But deriving from noncopyable is simpler and clearer, and doesn't require additional documentation."
http://www.boost.org/libs/utility/utility.htm#Class_noncopyable
一个具体的优点(除了更清楚地表达您的意图之外)是,如果成员或友元函数尝试复制对象,则在编译阶段而不是链接阶段会更快地捕获错误。基类构造函数/赋值在任何地方都不可访问,从而产生编译错误。
它还可以防止您意外定义函数(即输入
{}
而不是;
),这是一个很可能被忽视的小错误,但可以让成员和朋友制作对象的无效副本。One concrete advantage (beyond expressing your intent slightly more clearly) is that the error will be caught sooner, at the compile stage not the link stage, if a member or friend function tries to copy an object. The base-class constructor/assignment are not accessible anywhere, giving a compile error.
It also prevents you accidentally defining the functions (i.e. typing
{}
instead of;
), a small error which may well go unnoticed, but which would then allow members and friends to make invalid copies of the object.一个小缺点(特定于GCC)是,如果您使用
g++ -Weffc++
编译程序并且您有包含指针的类,例如GCC无法理解发生了什么:
虽然它不会抱怨:
PS 我知道 GCC 的 -Weffc++ 有几个问题。无论如何,检查“问题”的代码非常简单......有时它会有所帮助。
A small disadvantage (GCC specific) is that, if you compile your program with
g++ -Weffc++
and you have classes containing pointers, e.g.GCC doesn't understand what's happening:
While it won't complain with:
PS I know GCC's -Weffc++ has several issues. The code that checks for "problems" is pretty simplicistic, anyway... sometimes it helps.
优点是您不必自己编写私有复制构造函数和私有复制运算符,并且它可以清楚地表达您的意图,而无需编写额外的文档。
The advantage is that you don't have to write a private copy constructor and a private copy operator yourself and it expresses clearly your intention without writing additional documentation.
我宁愿使用 boost::noncopyable 而不是手动删除或私有化复制构造函数和赋值运算符。
然而,我几乎从不使用任何一种方法,因为:
如果我正在创建一个不可复制的对象,那么它是不可复制的就必须有一个原因。这个原因,99%的情况,是因为我有无法有意义复制的成员。有可能,此类成员也更适合作为私有实施细节。所以我制作大多数这样的类:
现在,我有一个私有实现结构,并且由于我使用了 std::unique_ptr,所以我的顶级类是不可免费复制的。由此产生的链接错误是可以理解的,因为它们讨论了如何无法复制 std::unique_ptr。对我来说,这就是 boost::noncopyable 和私有实现的所有好处。
这种模式的好处是稍后,如果我决定确实想让此类的对象可复制,我可以添加并实现复制构造函数和/或赋值运算符,而无需更改类层次结构。
I'd rather use boost::noncopyable than manually delete or privatize the copy constructor and assignment operator.
However, I almost never use either method, because:
If I am making a non-copyable object, there has to be a reason it is non-copyable. This reason, 99% of the time, is because I have members that can't be copied meaningfully. Chances are, such members would also be better suited as private implementation details. So I make most such classes like this:
So now, I have a private implementation struct, and since I've used std::unique_ptr, my top-level class is non-copyable for free. The link errors that come from this are understandable because they talk about how you can't copy a std::unique_ptr. To me, this is all the benefits of boost::noncopyable and a private implementation rolled into one.
The benefit with this pattern is later, if I decide that I did indeed want to make my objects of this class copyable, I can just add and implement a copy constructor and/or assignment operator without changing the class hierarchy.
缺点,根据斯科特·迈耶斯的说法,如果你确实需要找到它的缺点的话,这个名字是“非自然的”。
The disavantage, according to Scott Meyers, the name is "non-natrual", if you do need to find a disavantage of it.