c++c++17language-lawyercompiler-bugcopy-initialization

Ambiguous constructor error in gcc but not in msvc


I recently learnt that we can have more than one default constructor in a class. Then I wrote the following program that compiles with msvc but both clang and gcc fails to compile this.

struct A
{
  explicit A(int = 10);
  A()= default;
};

A a = {}; //msvc ok but gcc and clang fails here

Live demo

I want to know which compiler is correct here as per the C++17 standard.

GCC says:

<source>:8:8: error: conversion from '<brace-enclosed initializer list>' to 'A' is ambiguous
    8 | A a = {}; //msvc ok but gcc and clang fails here
      |        ^
<source>:5:3: note: candidate: 'constexpr A::A()'
    5 |   A()= default;
      |   ^
<source>:4:12: note: candidate: 'A::A(int)'
    4 |   explicit A(int = 10);

Solution

  • TLDR;

    This is CWG 2856 and the program is well-formed as per the current wording in the standard as descirbed below. Basically, only one of the ctors(A::A() and explicit A::A(int)) is a converting constructor. Thus only the former A::A() is the viable option and can be used.


    First note that A a = {}; is copy-initialization.

    1. The initialization that occurs in the = form of a brace-or-equal-initializer or condition ([stmt.select]), as well as in argument passing, function return, throwing an exception ([except.throw]), handling an exception ([except.handle]), and aggregate member initialization ([dcl.init.aggr]), is called copy-initialization.

    Next we move to the semantics of the initializer.

    1. The semantics of initializers are as follows. The destination type is the 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.

    The above means that A a = {}; is copy-list initiaization. Next we see the effect of list initialization:

    1. List-initialization of an object or reference of type 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 has either no default constructor ([class.default.ctor]) or a default constructor that is user-provided or deleted, then the object is default-initialized;

    This means that the object will be default initialized:

    1. 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.

    1. 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 that is not in the context of copy-initialization, the candidate functions are all the constructors of the class of the object being initialized. For copy-initialization, the candidate functions are all the converting constructors of that class. The argument list is the expression-list or assignment-expression of the initializer.

    This means that only the non-explicit ctor A::A() is the candidate because it is a converting ctor while the other explicit A::A(int) is not. Thus the set of candidates and viable options only contains one ctor A::A().

    Finally, since we only have one viable option, it is the one that is selected for the initializer ().