6

If the class doesn't have the constructor, will the compiler make one default constructor for it ?

Programmers new to C++ often have two common misunderstandings:

That a default constructor is synthesized for every class that does not define one

from the book Inside the C++ Object Model

I am at a loss...

MSalters
  • 173,980
  • 10
  • 155
  • 350
Eric.Q
  • 703
  • 1
  • 9
  • 21
  • AFAIK every class has a default constructor, however it is best to create it yourself (at least I do it). – Michel Keijzers Mar 09 '12 at 14:36
  • 1
    This quote is at the end of a section that explains at length when exactly a default constructor is synthesized. You should probably read this section and not jump directly to the end. – Luc Touraille Mar 09 '12 at 14:42
  • Yes, It is not a good question at all...I just want to listen what will you guys say – Eric.Q Mar 09 '12 at 14:46
  • 2
    You need to make your question understandable so we'll actually have something useful to say - but I think the book is getting at problems people run into when stuff that they expect to automatically happen doesn't happen, though either the book doesn't explain very well or there's a lot more content before they throw that line out there. – wkl Mar 09 '12 at 14:48
  • As Luc Touraille states in his answer: http://stackoverflow.com/a/9637074/297696 - this is definitely described in the book, and has a different meaning from what we thought. – wkl Mar 09 '12 at 16:17

6 Answers6

14

This is well explained in the section from which this quote is taken. I will not paraphrase it in its entirety, but here is a short summary of the section content.

First of all, you need to understand the following terms: implicitly-declared, implicitly-defined, trivial, non-trivial and synthesized (a term that is used by Stanley Lippman, but is not used in the standard).

implicitly-declared

A constructor is implicitly-declared for a class if there is no user-declared constructor in this class. For example, this class struct T { }; does not declare any constructor, so the compiler implicitly declares a default constructor. On the other hand, this class struct T { T(int); }; declares a constructor, so the compiler will not declare an implicit default constructor. You will not be able to create an instance of T without parameters, unless you define your own default constructor.

implicitly-defined

An implicitly-declared constructor is implicitly-defined when it is used, i.e. when an instance is created without parameters. Assuming the following class struct T { };, the line T t; will trigger the definition of T::T(). Otherwise, you would have a linker error since the constructor would be declared but not defined. However, an implicitly-defined constructor does not necessarily have any code associated with it! A default constructor is synthesized (meaning that some code is created for it) by the compiler only under certain circumstances.

trivial constructor

An implicitly-declared default constructor is trivial when:

  • its class has no virtual functions and no virtual base classes and
  • its base classes have trivial constructors and
  • all its non-static members have trivial constructors.

In this case, the default compiler has nothing to do, so there is no code synthesized for it. For instance, in the following code

struct Trivial
{
    int i;
    char * pc;
};

int main()
{
    Trivial t;
}

the construction of t does not involve any operations (you can see that by looking at the generated assembly: no constructor is called to construct t).

non-trivial

On the other hand, if the class does not meet the three requirements stated above, its implicitly-declared default constructor will be non-trivial, meaning that it will involve some operations that must be performed in order to respect the language semantics. In this case, the compiler will synthesize an implementation of the constructor performing these operations.

For instance, consider the following class:

struct NonTrivial
{
    virtual void foo();
};

Since it has a virtual member function, its default constructor must set the virtual table pointer to the correct value (assuming the implementation use a virtual method table, of course).

Similarly, the constructor of this class

struct NonTrivial
{
    std::string s;
};

must call the string default constructor, as it is not trivial. To perform these operations, the compiler generates the code for the default constructor, and calls it anytime you create an instance without parameters. You can check this by looking at the assembly corresponding to this instantiation NonTrivial n; (you should see a function call, unless the constructor has been inlined).


Summary

When you don't provide any constructor for your class, the compiler implicitly declares a default one. If you try to use it, the compiler implicitly defines it, if it can (it is not always possible, for instance when a class has a non-default-constructible member). However, this implicit definition does not imply the generation of any code. The compiler needs to generate code for the constructor (synthesize it) only if it is non-trivial, meaning that it involves certain operations needed to implement the language semantics.


N.B.

Stanley B Lippman's "Inside the C++ object model" and this answer deals with (a possible) implementation of C++, not its semantics. As a consequence, none of the above can be generalized to all compilers: as far as I know, an implementation is perfectly allowed to generate code even for a trivial constructor. From the C++ user point of view, all that matters is the "implicitly-declared/defined` aspect (and also the trivial/non-trivial distinction, as it has some implications (for instance, an object of a class with non-trivial constructor cannot be a member of a union)).

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
  • +1 - Interesting, interesting. Guess I need to take a look at this book. – wkl Mar 09 '12 at 16:02
  • wait, whats the synthesized mean? – Mooing Duck Mar 09 '12 at 16:55
  • Summary is incorrect. There are several more conditions on availability of a default constructor than simply "no user-declared constructors". – Ben Voigt Mar 09 '12 at 17:00
  • @BenVoigt: I may have forgot some conditions, could you elaborate? I read this sentence in the C++03 standard (§12.1.5) and did not dig further: `If there is no user-declared constructor for class X, a default constructor is implicitly-declared.`. – Luc Touraille Mar 09 '12 at 17:24
  • @MooingDuck: `synthesized` mean (in Stanley Lippman's book) that some code is generated for it by the implementation (I did not feel the need to make a paragraph to explain it as I introduce the notion in the `implicitly-defined` paragraph). – Luc Touraille Mar 09 '12 at 17:28
  • @LucTouraille: Ah sorry. I knew the other terms so I didn't actually read those paragraphs. – Mooing Duck Mar 09 '12 at 17:37
  • @LucTouraille: in your Summary,"the compiler implicitly declares a default one", so the there must be a constructor declared, but only defined if necessary?Is that right? – Eric.Q Mar 10 '12 at 03:38
  • @user974349: That's right, the constructor is defined only when you use it to create an object. – Luc Touraille Mar 10 '12 at 09:31
11

I think the misconception is:

That a default constructor is synthesized for every class that does not define one

That people think the default constructor, which accepts no arguments, will always be generated if you don't declare it yourself.

However, this is not true, because if you declare any constructor yourself, the default one will not be automatically created.

class MyClass {
public:
    MyClass(int x) {}; // No default constructor will be generated now
};

This will lead to problems like when beginners expect to use MyClass like this:

MyClass mc;

Which won't work because there is no default constructor that accepts no args.

edit as OP is still a little confused.

Imagine that my MyClass above was this:

class MyClass {
};

int main() {
    MyClass m;
}

That would compile, because the compiler will autogenerate the default constructor MyClass() because MyClass was used.

Now take a look at this:

#include <iostream>

class MyClass {

};

int main() {
    std::cout << "exiting\n";
}

If this were the only code around, the compiler wouldn't even bother generating the default constructor, because MyClass is never used.

Now this:

#include <iostream>

class MyClass {
public:
    MyClass(int x = 5) { _x = x; }
    int _x;
};

int main() {
    MyClass m;
    std::cout << m._x;
}

The compiler doesn't generate default constructor MyClass(), because the class already has a constructor defined by me. This will work, and MyClass(int x = 5) works as your default constructor because it can accept no arguments, but it wasn't generated by the compiler.

And finally, where beginners might run into a problem:

class MyClass() {
public:
    MyClass(int x) { _x = x; }
    int _x;
};

int main() {
    MyClass m;
}

The above will throw you an error during compilation, because MyClass m needs a default constructor (no arguments) to work, but you already declared a constructor that takes an int. The compiler will not generate a no-argument constructor in this situation either.

wkl
  • 77,184
  • 16
  • 165
  • 176
  • 3
    @user974349 - a Default Constructor in C++ has a very clear definition: it's a constructor which accepts no arguments (or all arguments have default arguments). – wkl Mar 09 '12 at 14:43
  • 1
    Not to be confused with a "defaulted default constructor", which is either the implicitly defined default constructor or a default constructor that is explicitly defaulted with `= default". – Joseph Mansfield Mar 09 '12 at 14:49
  • 1
    As a matter of fact, the misconception evoked by Stanley Lippman is not about the implicit declaration/definition of the default constructor, but about its synthesization (generation of some code for it). See [my answer](http://stackoverflow.com/a/9637074/20984). – Luc Touraille Mar 09 '12 at 15:59
4

A default constructor is synthesized for every class that does not define one if:

  • The code using the class needs one & only if
  • There is no other constructor explicitly defined for the class by you.
Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • when the users need one? A example? class A{} int main(){A a; return 0;} – Eric.Q Mar 09 '12 at 14:38
  • yep. 'A a;' requires a default ctor. So one is generatoed. – Carsten Greiner Mar 09 '12 at 15:33
  • The rules are rather strict here. `if (false) { A a; }` also requires a default ctor. The compiler doesn't look at the context where `a` is defined. – MSalters Mar 09 '12 at 15:50
  • In fact, the default constructor is implicitly *defined* when it is used, but that does not mean it is *synthesized* (in the sense Stanley Lippman uses the word). Indeed, if the constructor is trivial, it does not involve any operations and there is no need to generate any code for it, nor to make any function call when constructing an instance of the class (see [my answer](http://stackoverflow.com/a/9637074/20984)). – Luc Touraille Mar 09 '12 at 16:02
3

All the upvoted answers thus far seem to say approximately the same thing:

A default constructor is synthesized for every class that does not have any user-defined constructor.

which is a modification of the statement in the question, which means

A default constructor is synthesized for every class that does not have a user-defined default constructor.

The difference is important, but the statement is still wrong.

A correct statement would be:

A default constructor is synthesized for every class that does not have any user-defined constructor and for which all sub-objects are default-constructible in the context of the class.

Here are some clear counter-examples to the first statement:

struct NoDefaultConstructor
{
     NoDefaultConstructor(int);
};

class Surprise1
{
     NoDefaultConstructor m;
} s1; // fails, no default constructor exists for Surprise1

class Surprise1 has no user-defined constructors, but no default constructor is synthesized.

It doesn't matter whether the subobject is a member or a base:

class Surprise2 : public NoDefaultConstructor
{
} s2; // fails, no default constructor exists for Surprise2

Even if all subobjects are default-constructible, the default constructor has to be accessible from the composite class:

class NonPublicConstructor
{
protected:
    NonPublicConstructor();
};

class Surprise3
{
    NonPublicConstructor m;
} s3; // fails, no default constructor exists for Surprise3
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • I don't think this is correct. `Surprise1` **does** have an implicitly-defined constructor, but this constructor tries to call `NoDefaultConstructor::NoDefaultConstructor()`, which does not exist. The synthesized default constructor invokes a non-existent function, hence the compiler error. In Comeau, when you compile your samples, the error message states that `no default constructor exists for class "NoDefaultConstructor"`, not that `no default constructor exists for class "Surprise"`. – Luc Touraille Mar 09 '12 at 17:46
  • GCC outputs `no matching function for call to ‘NoDefaultConstructor::NoDefaultConstructor()’`. I think these two compilers have it right, as the standard is quite clear on this point: `If there is no user-declared constructor for class X, a default constructor is implicitly-declared.` (§12.1.5) What compiler did you use to test your samples? – Luc Touraille Mar 09 '12 at 17:50
  • @Luc: The compiler is trying to synthesize `Surprise1::Surprise1`, and it fails to do so. To just says "the compiler synthesizes it" implies success. Also, the standard says that in such cases the default constructor is implicitly defined as *deleted*. It does not exist. – Ben Voigt Mar 09 '12 at 18:25
  • @Luc: Your other comment says that "synthesizes" means that "code is generated". No code is generated here, because it would be illegal to do so. – Ben Voigt Mar 09 '12 at 18:26
  • I guess it depends what we mean by "code is generated". In Lippman's book, the implementation generates C code that is then compiled to machine code, so when he talks about code synthesis, he means that C code is generated. In our case, the C code could be generated but the compilation to machine code would fail. I don't know how this works on modern compilers, which does not use an intermediary C code: does the compiler generate an internal representation of the constructor ("synthesize" it), and then fails to transform that into machine code? Or does it fail when trying to "generate"... – Luc Touraille Mar 09 '12 at 19:28
  • ...the internal representation, and provides an error that explains why it can't? I'm not enough versed into compiler intrinsics to answer. Anyway, that does not really change anything from a user point of view: whether `Suprise` has an invalid default constructor or no default constructor at all makes little difference. However, I'll be interested to read the paragraph of the standard (C++11, I guess?) you quote, could you provide a reference? +1 btw, as your answer provides an important precision. – Luc Touraille Mar 09 '12 at 19:28
  • @Luc: Since C has no notion of access control, I find the argument that the compiler synthesizes C code, which then fails to compile, utterly unconvincing. Unfortunately my copy of C++11 is on my other computer, I can't find the particular section right now. – Ben Voigt Mar 09 '12 at 19:36
  • 1
    @Luc: It's 12.1p5, "A defaulted default constructor for class X is defined as deleted if...". Then p6: "A default constructor that is defaulted and not defined as deleted is implicitly defined when it is used" – Ben Voigt Mar 09 '12 at 19:46
  • 1
    You were right, the default constructor of `Surprise` is never defined, even before C++11. `Before the implicitly-declared default constructor for a class is implicitly defined, all the implicitly-declared default constructors for its base classes and its nonstatic data members shall have been implicitly defined.` (12.1p7). I just saw that the new standard made all this much more precise. Sorry for the long discussion! – Luc Touraille Mar 10 '12 at 09:37
0

Yes a default constructor is always there by default if you don't define a constructor of your own (see the default constructor section here).

talnicolas
  • 13,885
  • 7
  • 36
  • 56
  • No, the book said that's the new ones' misunderstandings – Eric.Q Mar 09 '12 at 14:36
  • If he defines another constructor, the default constructor isn't synthesised. – juanchopanza Mar 09 '12 at 14:40
  • Sure, but you're referring to the default constructor, so "if you don't define one" means "if you don't define a default constructor". – juanchopanza Mar 09 '12 at 14:44
  • @talnicolas - it is easy to misconstrue your statement as the incorrect "if you don't define one default constructor" rather than the more accurate "if you don't define one constructor." – Robᵩ Mar 09 '12 at 14:46
0

http://www.codeguru.com/forum/archive/index.php/t-257648.html

Quote:

The following sentense are got from the book "Inside the C++ object model" , written by Stanley B. Lippman.

There are four characteristics of a class under which the compiler needs to synthesize a default constructor for classes that declare no constructor at all. The Standard refers to these as implicit, nontrivial default constructors. The synthesized constructor fulfills only an implementation need. It does this by invoking member object or base class default constructors or initializing the virtual function or virtual base class mechanism for each object. Classes that do not exhibit these characteristics and that declare no constructor at all are said to have implicit, trivial default constructors. In practice, these trivial default constructors are not synthesized. ... Programmers new to C++ often have two common misunderstandings:

  • That a default constructor is synthesized for every class that does not define one

  • That the compiler-synthesized default constructor provides explicit default initializers for each data member declared within the class

As you have seen, neither of these is true.

Luc Touraille
  • 79,925
  • 15
  • 92
  • 137
Chriseyre2000
  • 2,053
  • 1
  • 15
  • 28
  • Please do not answer with random URLs. A good answer can point to a "for more information" resource, but should stand by itself. – MSalters Mar 09 '12 at 15:53