Is it possible to initialize member variable (or base class) of a non-copyable type?
So, I think I found the relevant parts of the standard and I think the compilers are in error regarding to X
. (All links are to a standard draft so very maybe it was different in C++17, I will check that later. But gcc10 and clang10 also fail with -std=c++20
, so that is not that important).
Regarding the initialization of base classes (emphasis mine): 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.
I think this tells us, that X() : S(foo()) {}
should not be different from S s = foo()
, but let's look at dcl.init/17.6.1
If the initializer expression is a prvalue and the cv-unqualified version of the source type is the same class as the class of the destination, the initializer expression is used to initialize the destination object. [Example:
T x = T(T(T()));
calls theT
default constructor to initializex
. — end example]
This implies to me, that X() : S(foo()) {}
should call the default constructor. I also tested (to be completely in line with the example) X() : S(S()) {}
and this also fails on clang and g++. So it seems to me that the compilers have a defect.