default argument, gcc vs clang

前端 未结 3 1670
余生分开走
余生分开走 2021-01-01 16:27

Code looks like:

struct Foo {
    Foo(const char *);
};

Foo::Foo(const char *str = 0)
{
}

VS 2013 and gcc 4.8.0 accept such code, while cl

相关标签:
3条回答
  • 2021-01-01 16:50

    This has been discussed on the Clang mailinglist and has been submitted as a Defect Report Core Issue 1344.

    From the mailinglist discussion:

    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.

    TL;DR Clang is right whenever the defect gets fixed (not sure if that can officially only happen with C++14, or if such Committee decisions can also be done retroactively on C++11)

    0 讨论(0)
  • 2021-01-01 16:51

    You have a declaration and a definition. In your declaration you do not have a default value, while in your definition you have a default value. In fact the signature of the declaration is very similar to the signature of the definition, but not the same. I believe that strictness is a good idea, so I believe it is better to enforce that the declaration is the same as the definition.

    0 讨论(0)
  • 2021-01-01 17:11

    I would say CLANG is right. The standard says (12.1.5 for the both old and new versions of the standard):

    A default constructor for a class X is a constructor of class X that can be called without an argument

    Adding the default value to the only argument of the constructor definitely makes it possible to call it without arguments, thus making it a default one. Also, 8.3.6 says (emphasis mine):

    A default argument expression shall be specified only in the parameter-declaration-clause of a function declaration <...>

    0 讨论(0)
提交回复
热议问题