[cfe-dev] Reason for Diagnostic on default ctors redeclaration
John McCall
rjmccall at apple.com
Tue May 14 16:30:28 PDT 2013
On May 14, 2013, at 3:29 PM, Alireza Moshtaghi <Alireza.Moshtaghi at synopsys.com> wrote:
> g++ accepts this code , but clang generates:
> error1.cpp:5:12:
> error: addition of default argument on redeclaration makes this
> constructor a default constructor
> “
> struct XX {
> int a;
> XX(int);
> };
> XX::XX(int c = 5) : a(c) { }
> “
> Question: where does this requirement come from ?
DR1344 is a defect report reported to the C++ standards committee:
http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1344
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.
John.
More information about the cfe-dev
mailing list