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?

5
  • I was expecting this to be a duplicate, actually, but I can't find this answered anywhere. Commented Nov 7, 2015 at 21:04
  • It's almost answered in Default parameters with C++ constructors, I think, but I'm not sure. Commented Nov 7, 2015 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. Commented Nov 7, 2015 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. Commented Nov 7, 2015 at 21:11
  • Yes, I'm not concerned with explicit for this question, but it's a thing to be careful of. Commented Nov 7, 2015 at 21:11

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Though there is a small difference for ABI-compatibility to consider.
@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".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.