User-declared default constructor + initializers in the class! = User-created constructor?

The Clang documentation clearly explains that

If a class or structure does not have a custom default constructor, C ++ does not allow you to build a const instance like this ([dcl.init], p9)

The rationale is that if the const object is not properly initialized, it cannot be changed later. The following code has only the default constructor declared for Test , but all its members have initializers in the class,

 #include<iostream> class Test { public: Test() = default; void print() const { std::cout << i << "\n"; } private: int i = 42; // will propagate to the default constructor! }; int main() { Test const t; // <-- Clang chokes on the const keyword, g++ does not t.print(); // prints 42 } 

therefore, the rationale for user- providing the default constructor seems superfluous to me. Indeed, g ++ 4.8.1 will compile it without problems ( Online Example ), although Clang <= 3.2 does not.

Questions : why the combination of full inital initializers + the default constructor declared by the user is not enough to build a const object by default? Is there a fix for C ++ 14 Standard?

UPDATE : can anyone try Clang 3.3 / 3.4 to see if this is fixed compared to Clang 3.2?

+13
c ++ c ++ 11 c ++ 14 default-constructor
Jul 05 '13 at 22:20
source share
1 answer

Yes, this is a known issue. See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#253 . It has not yet been fixed in the specification.

+11
Jul 05 '13 at 22:42
source share



All Articles