Question Details

No question body available.

Tags

c++ templates language-lawyer lazy-evaluation compiler-bug

Answers (1)

Accepted Answer Available
Accepted Answer
December 11, 2025 Score: 19 Rep: 47,814 Quality: Expert Completeness: 100%

Is GCC right when it accepts

No, GCC is incorrect (old GCC bug) in accepting an ill-formed program since Wrapper w{}; is direct initialization and it needs an existing default constructor for Wrapper w{}; to be valid but a default constructor will not be synthesized for Wrapper since you've provided your own constructor, as explained below in detail.


First note that Wrapper w{}; is direct initialisation(direct list initialization to be more specific). From dcl.init:

The initialization that occurs:

  • for an initializer that is a parenthesized expression-list or a braced-init-list,

is called direct-initialization.

Next we move to the semantics of the initializer.

  1. The semantics of initializers are as follows. The destination type is the cv-unqualified type of the object or reference being initialized and the source type is the type of the initializer expression. If the initializer is not a single (possibly parenthesized) expression, the source type is not defined.
  • If the initializer is a (non-parenthesized) braced-init-list or is = braced-init-list, the object or reference is list-initialized ([dcl.init.list])..

The above means that, the object is to be list-initialized.

From list initialization:

List-initialization is initialization of an object or reference from a braced-init-list. Such an initializer is called an initializer list, and the comma-separated initializer-clauses of the initializer-list or designated-initializer-clauses of the designated-initializer-list are called the elements of the initializer list. An initializer list may be empty. List-initialization can occur in direct-initialization or copy-initialization contexts; list-initialization in a direct-initialization context is called direct-list-initialization and list-initialization in a copy-initialization context is called copy-list-initialization. Direct-initialization that is not list-initialization is called direct-non-list-initialization.

The above means that Wrapper w{}; is direct-list initiaization. Next we see the effect of list initialization:

List-initialization of an object or reference of type cv T is defined as follows:

  • Otherwise, if the initializer list has no elements and T is a class type with a default constructor, the object is value-initialized.

The above means that the object will be value initialized, so we move on to value-initialization:

  1. To value-initialize an object of type T means:
  • If T is a (possibly cv-qualified) class type ([class]), then let C be the constructor selected to default-initialize the object, if any. If C is not user-provided, the object is first zero-initialized. In all cases, the object is then default-initialized.;

This means that the object will be default initialized:

To default-initialize an object of type T means:

  • If T is a (possibly cv-qualified) class type ([class]), constructors are considered. The applicable constructors are enumerated ([over.match.ctor]), and the best one for the initializer () is chosen through overload resolution ([over.match]). The constructor thus selected is called, with an empty argument list, to initialize the object.

So we move on to over.match.ctor to get a list of candidate ctors. Also do note the initializer() part in the above quoted reference as it will be used at the end as an argument.

When objects of class type are direct-initialized, copy-initialized from an expression of the same or a derived class type ([dcl.init]), or default-initialized, overload resolution selects the constructor. For direct-initialization or default-initialization (including default-initialization in the context of copy-list-initialization), the candidate functions are all the constructors of the class of the object being initialized. Otherwise, the candidate functions are all the non-explicit constructors ([class.conv.ctor]) of that class. The argument list is the expression-list or assignment-expression of the initializer. For default-initialization in the context of copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed.

But note that there is no default constructor for Wrapper. As you've provided your own parameterized ctor, the compiler won't synthesize a default ctor for Wrapper. Thus, the condition for Wrapper w{}; to be valid(which requires a default ctor) is not fulfilled and clang is correct in rejecting the program.

Important Note

Note that this is not IFNDR (aka ill-formed no diagnostic required) as the conditions in temp#res.general-6 aren't satisfied. This means that the compiler is required to emit a diagnostic which is why GCC is wrong since it doesn't emit any diagnostic for this ill-formed program.