Is there any difference between a default user-defined constructor
class Simple
{
public:
Simple() {}
};
and a user-defined constructor that takes multiple arguments but has defaults for each of these
class WithDefaults
{
public:
WithDefaults(int i = 1) {}
};
other than that WithDefaults
can also be constructed with an explicit value for i
?
Specifically, I am wondering, as far as the language is concerned, if these two constructors play the exact same roll of default constructor for both, or if there are subtle differences between the properties of the classes?
In other words, is a constructor which has default values for all of its arguments a default constructor in every way?
Current Standard working draft N4527 [12.1p4]:
A default constructor for a class
X
is a constructor of classX
that either has no parameters or else each parameter that is not a function parameter pack has a default argument. [...]
So yes, the constructor of the second class is a perfectly valid default constructor.
Just a note that the wording in the published versions of C++11 and 14 was slightly different, but doesn't make a difference for your question. It used to be:
A default constructor for a class
X
is a constructor of classX
that can be called without an argument.
The change to the current wording was made as a result of DR 1630, in order to clarify the semantics of default initialization. Previously, there were places in the standard that referred to "the default constructor", implying that there can be only one; the current wording is intended to support more complex scenarios, where you can potentially have several such constructors (for example using SFINAE), and the one used is chosen using normal overload resolution.