Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
342 views
in Technique[技术] by (71.8m points)

c++ - Does the default constructor of std::pair<> set basic types (int, etc) to zero?

After writing:

std::pair<int, int> x;

Am I guaranteed that x.first and x.second are both zero? Or could they have any value?

The reason why I care is because I'm trying to determine whether a map whose values are pointers is guaranteed to return NULL if I access an element that's not in the map. I.e., if I do:

std::map<int, void*> my_map;
std::cout << int(my_map[5]) << std::endl;

then am I guaranteed to get zero (NULL)? Or is the behavior undefined?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes, that guarantee holds true. Quoting the C++11 standard, §20.3.2/2-3:

        constexpr pair();

2 Requires: is_default_constructible<first_type>::value is true and is_default_constructible<second_type>::value is true.
3 Effects: Value-initializes first and second.

And §8.5/7:

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type with a user-provided constructor, 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 lastly, §8.5/5:

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

  • if T is a scalar type, the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;
  • if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;
  • if T is a (possibly cv-qualified) union type, the object’s first non-static named data member is zero-initialized and padding is initialized to zero bits;
  • if T is an array type, each element is zero-initialized;
  • if T is a reference type, no initialization is performed.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...