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.
- 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.
- 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:
- 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:
- 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:
- 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 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 ().
explicit
. – Collen{}
is an implicit object creation. not sure which is correct here – Foreglimpseconstexpr
qualified, thereby avoiding another clash in the first place. – Antrim10
, unless there is an attempt to invoke that constructor. All it sees isexplicit A(int);
, which, of course, does not conflict withA()
. If, however, you attempt to invoke the default constructor (without the legerdemain of copy-initialization), then MSVC notices that there are two default constructors. – Gidgetgiemain
defines a variable like this:A b;
, you will get a duplicate definition error. This rubs me wrong. I would like the compiler to emit an error whether or not any objects of classA
are declared. There are two default constructors, and I would like the compiler to say something about that. – GidgetgieA
:void test(int = 10) {}
andvoid test() {}
If you call the parameterless version,test()
, you will get a duplicate definition error. If, however, you always supply an integer argument, the two functions compile in MSVC, and the program runs. Just like the constructors, MSVC does not see the default parameter value10
, unless you force it to. – Gidgetgie