Does the list of initializers include a constructor call?

If I declare a class with a standard constructor and define an instance of this class with a list of initializers, as shown below, will the default constructor be called for this definition? And why were they called or not called?

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

int main()
{
  Sample s = {0};
  return 0;
}
+3
source share
5 answers

The standard says ($ 8.5 / 14)

The semantics of initializers is as follows. The destination type is the type of the initialized object or reference, and the source type is the type of the initializer expression. The type of the source is not determined when the initializer is enclosed in brackets or when it is a list of expressions in parentheses.

(, cv-) : - (8.5.1), , , . 8.5.1..

.

8.5.1/13

[. (12.1). 12.6.1. ]

12.6.1/2

( ) (8.5.1), , (. 8.5) -. , , , , (8.5).

+1
  • ++ 03 ,
  • ++ 0x ( std::initializer_list)
+5

, , , .

Such initialization is valid only for aggregates that cannot have constructors declared by the user, therefore, suppressing a constructor created by a compiler is almost academic.

+3
source

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

+1
source

In C ++, you can initialize POD (plain old data) with = {0} (at least until C ++ 0x). Therefore, the default constructor will not be called because it will not compile.

+1
source

Source: https://habr.com/ru/post/1777024/


All Articles