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?
As default constructor initializes the data members of class to 0.
Most programmers use “” as the default value for String variables and 0 as the default value for int and double variables. If there are no constructors written for a class, Java provides a no-argument default constructor where the instance variables are set to their default values.
No, the C++ compiler doesn't create a default constructor when we initialize our own, the compiler by default creates a default constructor for every class; But, if we define our own constructor, the compiler doesn't create the default constructor.
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
istrue
andis_default_constructible<second_type>::value
istrue
.
3 Effects: Value-initializesfirst
andsecond
.
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 forT
is called (and the initialization is ill-formed ifT
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, ifT
'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 value0
(zero), taken as an integral constant expression, converted toT
;- 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.
From C++11 standard, section § 20.3.2
constexpr pair(); ... Effects: Value-initializes first and second.
So it is well-defined that the default initialization of an std::pair<int, int>
object will result in both members being set to 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With