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
?
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.
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