0
#include <iostream>
#include <string>

int main() {
    std::pair<std::string, int> s;
    std::cout << s.second << std::endl;
}

In this example s.second is 0 though it is not initialized. Can you provide a link to C++ standard where is defined why is it 0. I know it is because s.second is initialized by int(), but cant found the line in standard where is stated that int() is 0.

Ashot
  • 10,807
  • 14
  • 66
  • 117
  • 2
    see also http://stackoverflow.com/questions/9025792/does-the-default-constructor-of-stdpair-set-basic-types-int-etc-to-zero – lcs Mar 21 '13 at 13:24

3 Answers3

6

It's

8.5 Initializers [dcl.init]

10) An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

and

7) To value-initialize an object of type T means:
— if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
— if T is a (possibly cv-qualified) non-union class type without a user-provided constructor, then the object is zero-initialized and, if T’s implicitly-declared default constructor is non-trivial, that constructor is called.
— if T is an array type, then each element is value-initialized;
— otherwise, the object is zero-initialized.

and I guess

5) To zero-initialize an object or reference of type T means:
— if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T; [...]

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
5

I know it is because s.second is initialized by int(), but cant found the line in standard where is stated that int() is 0.

Here is the path you have to follow in the C++11 Standard - this answer uses Draft n3485 as a reference, which is more recent than the current official Standard.

Per Paragraph 8.5/11 of the C++11 Standard:

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized. [...]

Moreover, per Paragraph 8.5/8 of the C++11 Standard:

To value-initialize an object of type T means:

— if T is a (possibly cv-qualified) class type (Clause 9) with either no default constructor (12.1) or a default constructor that is user-provided or deleted, then the object is default-initialized;

— if T is a (possibly cv-qualified) non-union class type without a user-provided or deleted default constructor, then the object is zero-initialized and, if T has a non-trivial default constructor, default-initialized;

— if T is an array type, then each element is value-initialized;

otherwise, the object is zero-initialized.

Finally (although this is quite intuitive), per Paragraph 8.5/6:

To zero-initialize an object or reference of type T means:

if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;

— [...]

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
3

I know it is because s.second is initialized by int()

The standard doesn't actually say that second is initialized with int(). It just gives the effect of creating a std::pair with the default constructor as (§20.3.2):

Effects: Value-initializes first and second.

Value-initialization is defined as (§8.5):

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9) [...]

  • if T is a (possibly cv-qualified) non-union class type [...]

  • if T is an array type, [...]

  • otherwise, the object is zero-initialized.

Whcih results in second being zero-initialized because it is an int (§8.5):

To zero-initialize an object or reference of type T means:

  • if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;

  • [...]

Community
  • 1
  • 1
Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324