我正在查看最近发布的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.
}
我误解了,还是书或编译器错了?