Is there a std::noncopyable (or equivalent)?

风流意气都作罢 提交于 2020-01-13 07:37:07

问题


There's a boost::noncopyable and I have my own noncopyable class in my library. Is there a std::noncopyable or equivalent knocking around in the latest C++ standard?

It's a small thing but deriving from such a class makes the intention much clearer.


回答1:


No, because there is a standard way to make a class non-copyable:

class MyClass
{
   MyClass(const MyClass&) = delete;
   MyClass& operator=(const MyClass&) = delete;
}:

A class that is non-copyable can however be made movable by overloading a constructor from MyClass&&.

The declaration to make the class non-copyable (above) can be in the public or private section.




回答2:


I'm sure you already figured it out by reading CashCow's post, but I thought I might as well provide a base class for noncopyable classes.

class Noncopyable {
public:
    Noncopyable() = default;
    ~Noncopyable() = default;

private:
    Noncopyable(const Noncopyable&) = delete;
    Noncopyable& operator=(const Noncopyable&) = delete;
};


来源:https://stackoverflow.com/questions/31940886/is-there-a-stdnoncopyable-or-equivalent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!