6

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?

MicroVirus
  • 5,324
  • 2
  • 28
  • 53
  • I was expecting this to be a duplicate, actually, but I can't find this answered anywhere. – MicroVirus Nov 07 '15 at 21:04
  • It's *almost* answered in [Default parameters with C++ constructors](http://stackoverflow.com/questions/187640/default-parameters-with-c-constructors), I think, but I'm not sure. – MicroVirus Nov 07 '15 at 21:07
  • 1
    In isolation, I think, the classes are the same. They can act differently when conversions are taken into account. The link in the second comment by @MicorVirus explains this further. – Anon Mail Nov 07 '15 at 21:10
  • Awesome question! I don't supply this as a definitive answer, but in my experience it has always worked out that they are the same. – GreatAndPowerfulOz Nov 07 '15 at 21:11
  • Yes, I'm not concerned with `explicit` for this question, but it's a thing to be careful of. – MicroVirus Nov 07 '15 at 21:11

1 Answers1

6

Current Standard working draft N4527 [12.1p4]:

A default constructor for a class X is a constructor of class X 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 class X 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.

bogdan
  • 9,229
  • 2
  • 33
  • 48
  • Though there *is* a small difference for ABI-compatibility to consider. – Deduplicator Nov 07 '15 at 21:19
  • 2
    @Deduplicator A good point, but I guess that falls under the general statement "a function that has default arguments for all parameters can be called with the same syntax as one that doesn't have any, but that doesn't mean the two functions have the same signature". – bogdan Nov 07 '15 at 21:37