Reason for Diagnostic on default ctors redeclaration


g++ accepts this code , but clang generates:


error: addition of default argument on redeclaration makes this

constructor a default constructor

struct XX {

int a;



XX::XX(int c = 5) : a(c) { }

Question: where does this requirement come from ?

The diagnosis is generated in SemaDeclCXX.cpp:

// DR1344: If a default argument is added outside a class definition and that

// default argument makes the function a special member function, the program

// is ill-formed. This can only happen for constructors.

if (isa(New) &&





DR1344 is a defect report reported to the C++ standards committee:

We expect the committee to make some sort of modification here and
have pre-emptively locked things down to reserve more flexibility to
respond to whatever the committee does.

The idea is that the presence of certain special members affects core
properties of a class type, like whether it's POD or trivially copyable.
Deciding these properties should not require whole-program knowledge;
it's important for us to be able to deduce them just from the class definition.
The really problematic case is turning a "normal" constructor into a copy
or move constructor by adding default arguments, but IIRC introducing a
default constructor was also problematic.

The fix is that you should put the default argument in the initial declaration
of the constructor.


This was last discussed by WG21 at the Bloomington meeting. Notes from there:

“Consensus: Make this ill-formed as suggested in the write-up. Core issue 1344. Priority 0, Doug drafting.”

So CWG has agreed (in principle) that this should be ill-formed.