3

If I declare a class with default constructor and define a instance of this class with initializer list as below, will the default constructor be called for this definition? And why been called or not been called?

class Sample
{
// this should be any aggregate type in c++
};

int main()
{
  Sample s = {0};
  return 0;
}
Thomson
  • 20,586
  • 28
  • 90
  • 134

5 Answers5

5
  • In C++03, only aggregate classes may be initialized with curly braces, and an aggregate class may not have a user defined constructor
  • In C++0x, this syntax is supported for non aggregate types through initializer lists and calls the appropriate constructor (taking a std::initializer_list)
Community
  • 1
  • 1
icecrime
  • 74,451
  • 13
  • 99
  • 111
3

When you provide an brace enclosed initializer all the members of the class are copy-initialized from the corresponding expression of the brace enclosed initializer.

Such initialization is only valid for aggregates which cannot have user-declared constructors so the suppression of the compiler generated constructor is almost academic.

CB Bailey
  • 755,051
  • 104
  • 632
  • 656
1

The corresponding constructor is called: http://en.wikipedia.org/wiki/C%2B%2B0x#Initializer_lists

Šimon Tóth
  • 35,456
  • 20
  • 106
  • 151
1

In C++ you may only initialize POD (plain old data) with ={0} (at least pre C++0x). So the default constructor will not be called because this won't compile.

fmuecke
  • 8,708
  • 1
  • 20
  • 25
  • Not true. POD is more restrictive than aggregate. For example a class with a user-declared destructor can be initialized with a brace enclosed initializer. – CB Bailey Nov 26 '10 at 07:44
  • 1
    Just backing up @Charles, e.g. a raw array of `std::string` can be initialized using curly braces. The OP very precisely says *aggregate*. That's a term defined by the Holy Standard: aggregates can be initialized with curly braces notation. – Cheers and hth. - Alf Nov 26 '10 at 07:48
1

The Standard says ($8.5/14)

The semantics of initializers are as follows. The destination type is the type of the object or reference being initialized and the source type is the type of the initializer expression. The source type is not defined when the initializer is brace-enclosed or when it is a parenthesized list of expressions.

If the destination type is a (possibly cv-qualified) class type: — If the class is an aggregate (8.5.1), and the initializer is a brace-enclosed list, see 8.5.1. .

.

8.5.1/13

[Note: An aggregate array or an aggregate class may contain members of a class type with a user-declared constructor (12.1). Initialization of these aggregate objects is described in 12.6.1. ]

Also 12.6.1/2 says

When an aggregate (whether class or array) contains members of class type and is initialized by a brace-enclosed initializer-list (8.5.1), each such member is copy-initialized (see 8.5) by the corresponding assignment-expression. If there are fewer initializers in the initializer-list than members of the aggregate, each member not explicitly initialized shall be value-initialized (8.5).

Community
  • 1
  • 1
Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345