C++不可复制,除非有时
我发现使类不可复制对我的代码质量有很大帮助。最初我使用 boost::noncopyable 执行此操作,但我发现 VC++ 编译器错误不如私有成员那么有用(双击会导致代码中的错误位置)。
T(T const&);
T& operator=(T const&);
事实上,它提醒我很多情况下类没有作为参考传递到它们应该传递的地方。如此之多,以至于我非常希望收到警告,即使是在类上,我只需要复制构造一次。
有没有好的方法可以做到这一点?例如,我正在考虑将上述两个方法保留为私有,并添加一个公共 T(T const&,bool dummy) 构造函数以在我真正想要复制构造时调用。或者也许可以将上述两种方法公开,并在复制构造时以某种方式激活编译器警告,从而在我确实想要的地方抑制警告。
或者也许有一个更好的方法?
I find that making a class non-copyable helps me a lot with my code quality. Initially I did this with boost::noncopyable, but I found the VC++ compiler errors to be not as helpful as with private members (double clicking lead to the wrong place in the code).
T(T const&);
T& operator=(T const&);
Indeed, it has alerted me to quite a few cases were classes were not passed as reference where they should have. So much so, that I would very much like to get a warning even on classes that I just need to copy construct once.
Is there a good way to do this? I was thinking for example of leaving above two methods private and adding a public T(T const&,bool dummy) constructor to call when I really want to copy construct. Or maybe alternatively make above two methods public and somehow activate a compiler warning when copy constructing, suppressing the warning where I do want to.
Or maybe there is an all together better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不确定这是否正是您想要的,但如果您将复制构造函数标记为显式,则该类无法按值传递或复制初始化,但您可以使用直接初始化进行复制构造。
您可能希望将赋值运算符保持为私有,也许
NonAssignable
基对此很有用。Not sure if it's exactly what you want, but if you mark the copy constructor
explicit
then the class can't be passed by value or copy-initialized, but you can copy-construct using direct initialization.You'd presumably want to keep the assignment operator private, maybe a
NonAssignable
base would be useful for that.我认为你自己命名了一个完美的方式。
我刚刚记得我曾经在我工作的另一个代码库中玩过一个巧妙的(?)小技巧:
正如评论中所讨论的,以下内容不会飞
您可以选择显式名称(如 CopyConstruct)并依赖 RVO 来实现同样高效:
I think you named a perfect way there yourself.
I just remembered a neat(?) little trick I once played in another code base I worked on:
As discussed in the comments the following won't fly
You might opt for an explicit name (like CopyConstruct) and rely on RVO to be equally efficient:
您可以默认构造一个
T
,然后添加一个assign
方法来使用。但这似乎并不完全最佳,表明您可能需要检查您的复印需求。You could possibly default construct a
T
and then add anassign
method to use. This doesn't seem fully optimal though, indicating that you might want to review your copying needs.我不喜欢限制类型的想法(CopyConstructible 是整个 stdlib 中广泛使用的概念),因为它可能会被滥用。如果您可以从另一个实例构造一个对象,那么它应该是可复制构造的。它还会分散读者对重要代码的注意力,而没有任何实际目的。
也许复制构造函数触发的调试模式下的警告或断言确实是您正在寻找的?
I don't like the idea of restricting a type (
CopyConstructible
is a heavily used concept throughout the stdlib) because it might be misused. If you can construct an object from another instance, it should be copy constructible. It also distracts the reader from the important code without serving any real purpose.Maybe a warning or assertion in debug mode that is triggered by the copy constructor is really what you are looking for?