我想创建一个noncopyable和nonmovable的基类。
class noncopyable {
protected:
noncopyable() = default;
~noncopyable() = default;
noncopyable(noncopyable const &) = delete;
noncopyable &operator=(noncopyable const &) = delete;
};
class nonmovable {
protected:
nonmovable() = default;
~nonmovable() = default;
nonmovable(nonmovable &&) = delete;
nonmovable &operator=(nonmovable &&) = delete;
};
是否存在类应该是不可复制和不可移动的情况?
class non : public noncopyable, public nonmovable {
};
class foo : public non {
};
如果有,那么" non"的方便名称应该是什么?这里上课?
答案 0 :(得分:4)
有人认为,例如here,nonmoveable
首先证明是一个坏主意。
有四种合理的选择:
如果类型是可复制的,但移动它失败,这绝对是一个糟糕的安排,而应该移动后备复制。
表示,当副本没问题时,移动失败没有任何优势,它只会抑制泛型编程。所以也许你应该只有“noncopyable”和“non”,但不是“nonmoveable”?
答案 1 :(得分:3)
While a "noncopyable" will work, an "nonmovable" base class will not provide what you expect:
#include <utility>
#include <iostream>
struct nonmovable
{
nonmovable() = default;
nonmovable(const nonmovable&) { std::cout << "copy\n"; }
nonmovable& operator = (const nonmovable&) { std::cout << "asign\n"; return *this; }
nonmovable(nonmovable&&) = delete;
nonmovable& operator = (nonmovable&&) = delete;
};
struct X : nonmovable {};
int main()
{
nonmovable n0;
nonmovable n1(n0);
// error: use of deleted function ‘nonmovable::nonmovable(nonmovable&&)’:
//nonmovable n2(std::move(n0));
X x0;
X x1(x0);
// However, X has a copy constructor not applying a move.
X x2(std::move(x0));
}
In addition, move construction and move assignment must be enabled explicitly after deletion of the copy constructor, if desiered:
struct noncopyable
{
noncopyable() = default;
// Deletion of copy constructor and copy assignment makes the class
// non-movable, too.
noncopyable(const noncopyable&) = delete;
noncopyable& operator = (const noncopyable&) = delete;
// Move construction and move assignment must be enabled explicitly, if desiered.
noncopyable(noncopyable&&) = default;
noncopyable& operator = (noncopyable&&) = default;
};
The, names "noncopyable" and "nonmovable" itself are good descriptive names. However, "boost::noncopyable" is both (non copyable and non movable), which might be a better (historical) design decision.
答案 2 :(得分:-3)
as an example - signleton pattern. Also, if u defined copy-constructor/assignment operator/destructor, move-constructor/assignment wont be generated.