我正在浏览最近发布的 C++ 编程语言第 4 版,以了解新功能。我正在使用 VS2012 Update 3 (VC11) 编译器,(我知道这并不理想,但这是我必须使用的)。
17.6 生成默认操作状态:
• If the programmer declares any constructor for a class, the default constructor
is not generated for that class.
• If the programmer declares a copy operation, a move operation, or a destructor
for a class, no copy operation, move operation, or destructor is generated for
that class.
我对此的理解是,如果我为一个类声明了一个复制构造函数,那么编译器生成的复制和移动操作以及析构函数都应该被禁止。
然而在 VS2012 中,我看到了这个:
struct H{}; // Compiler generated default, copy and move constructors, and
// destructor.
struct J
{
explicit J(int){}
J(const J&){} // User copy constructor, so compiler generated default,
// copy or move operations and destructor should be
// suppressed.
};
void Foo()
{
H h1; // Compiler generated default construct.
H h2 = h1; // Compiler generated copy construct;
h1 = h2; // Compiler generated copy assign.
H h3 = move(h1); // Compiler generated move construct.
h3 = move(h2); // Compiler generated move assign.
// J j1; // Correct. Does not compile. No compiler generated default
// constructor.
J j2(1); // User constructor.
J j3 = j2; // User copy constructor.
j2 = j3; // Error? Generated copy assignment should be suppressed.
J j4 = move(j2); // Error? Generated move constructor should be suppressed.
j3 = move(j4); // Error? Generated move assignment should be suppressed.
}
那么是我误解了,还是书或编译器错了?