Search code examples
c++language-lawyerdefault-constructor

Is implicitly deleted default constructor same as Compiler not synthesizing the default constructor


I am learning about class in C++. I came to know that in certain situations the default constructor can be implicitly deleted. Also, i read that when we have a user defined constructor then the compiler will not automatically synthesize the default constructor.

To my current understanding, an implicitly deleted default constructor is quite different from the situation when the default constructor is not automatically synthesized. I mean in both cases we have the same effect, which is that we cannot create an object using default initialization. But still an implicitly deleted default constructor is not the same as compiler not synthesizing the default constructor.

But then i came across this answer which says:

when you explicitly declare any constructors, the default constructor is implicitly deleted.

(end of quote)

As i said, i don't think that when we have user defined constructors in our class then the default constructor is implicitly deleted. Instead the default constructor is not automatically synthesized by the compiler which is different(IMO) from being implicitly deleted. So the quoted answer seems technically wrong to me. And so the quoted answer should instead say that when we have user defined constructors then the compiler will not automatically synthesize the default ctor for us. But many people have upvoted(+marked it as correct) that answer so i may be missing something. Please correct me where i am wrong in my understanding of the topic and also tell me whether or not the quoted answer is technically correct.

My point is that "having an implicitly deleted default ctor" and "the compiler not synthesizing a default ctor due to the presence of user defined constructor" are 2 different things.


Solution

  • This is covered in [class.default.ctor]:

    If there is no user-declared constructor for class X, a non-explicit constructor having no parameters is implicitly declared as defaulted ([dcl.fct.def])

    So if there are user-declared constructors, there is no creation of an "implicitly declared" defaulted constructor.

    The broader question is this: why isn't an implicitly declared default constructor deleted instead of not added at all?

    Well, here's the definition of what a default constructor is:

    A default constructor for a class X is a constructor of class X for which each parameter that is not a function parameter pack has a default argument (including the case of a constructor with no parameters).

    Basically, a constructor is a default constructor if you can call it with no arguments. So it's not defined by a single signature; it's define by calling behavior.

    But a default constructor declaration is also a declaration of a user-declared constructor. So if you declare a default constructor, the compiler will not implicitly declare its own. Because otherwise you'd have two default constructors, which is... unhelpful when by definition they can both be called with no arguments (and therefore nothing to overload on) and one of them is deleted.

    So yes, it's incorrect to say that it is deleted if you provide a user-declared constructor.