12

Suppose I have a map

std::map<int, double> foo;

Is the behaviour on my writing foo[2] += 3.0; defined? That is, are any implicitly added map elements automatically initialised (hopefully to 0.0) in my case?

If not, am I introducing a truck-load of undefined behaviour? If so, could I do something funky with an allocator to enforce initialisation to 0.0?

1

3 Answers 3

8

Yes, it will be value-initialized (as 0.0 in your case). According to cppreference:

Returns a reference to the value that is mapped to a key equivalent to key, performing an insertion if such key does not already exist.

If an insertion is performed, the mapped value is value-initialized (default-constructed for class types, zero-initialized otherwise) and a reference to it is returned.

Sign up to request clarification or add additional context in comments.

6 Comments

"zero-initialized otherwise". That's the crux of it. Thank you.
Has this always been true, or just in c++11? C++11 does much more to fill in defaults than previously, but I don't know if this is one of those contexts
@AaronMcDaid This is also true in C++03.
@AaronMcDaid C++11 added more syntax for value initialization but the result of value initialization didn't change.
@songyuanyao, according to this something has changed from C++03 to C++11 in the meaning of new T(). "Scalars and POD types with dynamic storage duration were considered to be not initialized (since C++11, this situation was reclassified as a form of default initialization). " That's why I'm curious if C++03 really is the same as C++11 as regards this question, perhaps a similar change (similar to that quote) has been introduced for map elements. But I suppose if there was a difference, the page you linked would say so.
|
0

N3337 [map.access]/1 Effects: If there is no key equivalent to x in the map, inserts value_type(x,T()) into the map.

T() is value-initialization, which is the case of built-in types causes zero-initialization. As such, foo[2] will insert a zero-initialized double into your map, so your code is well-defined.

Comments

-1

Yes, they are automatically value initialized when using operator[] on a non existing key. Specifically in the standard it's described at §23.4.4.3/1 (when talking about operator[]):

Effects: If there is no key equivalent to x in the map, inserts value_type(x, T()) into the map.

For most numeric types, including double, the expression T() yields a value-initialized element of that type, therefore yielding 0.0 in your case.

6 Comments

But a double doesn't really have a default constructor.
They are actually value initialized, otherwise OP's code would invoke UB.
@juanchopanza It's actually the same thing for double.
I was thinking of default initialization, since you'd originally said it is default constructed. Default initialization isn't the same thing as value initialization.
And double does not have a default constructor.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.