13

The Clang documentation neatly explains that

If a class or struct has no user-defined default constructor, C++ doesn't allow you to default construct a const instance of it like this ([dcl.init], p9)

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

#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
}

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

Questions: why is the combination of complete in-class initalizers + user-declared default constructor not enough to default construct a const object? Is there a fix underway for the C++14 Standard?

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

TemplateRex
  • 69,038
  • 19
  • 164
  • 304
  • 2
    I think this is a mistake/bug in Clang, and g++ is correct to accept the code. – Jerry Coffin Jul 05 '13 at 22:36
  • @JerryCoffin Any Standard quote to go along with that? I ask because without the in-class initializer, the Standard says Clang is right. – TemplateRex Jul 05 '13 at 22:37
  • 1
    Not really a quote, but by N3337, the wording had already been changed so this requirement had disappeared. Given that it *is* in the standard, I guess calling it a mistake or bug is probably slightly inaccurate, but at this point I think most compilers are mostly ignoring C++11 *per se* and pursuing newer drafts. – Jerry Coffin Jul 05 '13 at 22:58
  • @JerryCoffin so is that DR that is marked as "active" already incorporated into the spec, contrary to what my answer states? With what solution? – Johannes Schaub - litb Jul 06 '13 at 10:44
  • @JohannesSchaub-litb: TBH, I'm not entirely sure -- the wording quoted in the DR seems to be gone, but that may be a side-effect of other editing, and they're still working on more changes to deal more specifically with this DR (or not -- without going back through meeting minutes I'm not sure why things changed to what they are now). – Jerry Coffin Jul 06 '13 at 15:19
  • @abyss.7 erm, how could I look into the future? ;-) – TemplateRex Feb 20 '14 at 11:41
  • @TemplateRex I've just read the questions about such situation on Meta - and the answers recommend to close the older question if it's not so informative. No offense at all. – abyss.7 Feb 20 '14 at 11:50
  • @abyss.7 no offense was taken, I was just amused ;-) – TemplateRex Feb 20 '14 at 11:51
  • As stated in my answer here: https://stackoverflow.com/a/47368753/852254 , this is resolved in clang in 3.9.0. – David Stone Nov 18 '17 at 18:03

1 Answers1

11

Yes, this is a known problem. See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#253 . It hasn't been fixed yet in the spec.

Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212