My c++ book (lippman, c++ primer, fifth ed., p. 508) provides these 4 rules for figuring out when the compiler will synthesize the copy control and default constructor as deleted members:
The synthesized destructor is defined as deleted if the class has a member whose own destructor is deleted or inaccessible (e.g. private).
The synthesized copy constructor is defined as deleted if the class has a member whose own copy constructor is deleted or inaccessible. It is also deleted if the class has a member with a deleted or inaccessible destructor.
The synthesized copy-assignment operator is defined as deleted if a member has a deleted or inaccessible copy-assignment operator, or if the class has a const or reference member.
The synthesized default constructor is defined as deleted if the class has a member with a deleted or inaccessible destructor; or has a reference member that does not have an in-class initializer; or has a const member whose type does not explicitly define a default constructor and that member does not have an in-class initializer.
I’m failing to see how these rules explain the SECOND error here:
class Foo {
public:
Foo(int i) { }
};
class Bar {
private:
Foo foo;
};
int main() {
Foo foo; //error: no matching constructor in Foo
Bar bar; //error: implicitly deleted constructor in Bar
return 0;
}
The first error is understandable and has nothing to do with this question directly. The second error is surprising because the above rules not explain why Bar should get its default constructor synthesized as deleted.
what rules is my book missing, or am I not grasping the rules?
Foohas no default constructor because you declare a constructor; from C++11 12.1/5:Barhas a deleted default constructor becauseFoohas no default constructor; from C++11 12.1/5 (5th bullet point):The “rules” you quote do seem to be missing that point, only mentioning the case of const-qualified members in the 3rd bullet point.