The following code fails to compile with both Gcc and Clang because of the copy construction of B
base class suboject inside A
constructor:
struct B{
B();
B(const B&) =delete;
};
struct A:B{
A():B(B()){} //=> error: use of deleted function...
};
Nevertheless according to [class.base.init]/7:
The expression-list or braced-init-list in a mem-initializer is used to initialize the designated subobject (or, in the case of a delegating constructor, the complete class object) according to the initialization rules of [dcl.init] for direct-initialization.
So the initialization rule is the same for members or direct bases. For a member subobject, Gcc and Clang do not use the deleted copy constructor:
struct A2:B{
B b;
A2():b(B()){} //=> OK the result object of B() is b
};
Is not it a compiler bug of both Clang and Gcc? Should not the copy constructor of B
be elided in A()
?
Interestingly, even if gcc checks if a copy construction is well formed, it elides this copy constructor call, see assembly here