I have code as below, I had try it in MSVC2010 which works ok.
std::map<int, int*> fooMap;
assert(fooMap[1] == null);
Did C++ Standard guarantee the assert would not failed forever?
thanks.
I have code as below, I had try it in MSVC2010 which works ok.
std::map<int, int*> fooMap;
assert(fooMap[1] == null);
Did C++ Standard guarantee the assert would not failed forever?
thanks.
When inserting into a std::map
using the operator[]
function, the data will be value initialized, and value initialization for pointers will make them zero (i.e. null pointers).
Yes.
From http://en.cppreference.com/w/cpp/container/map/operator_at
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.
Two Answer could be answered:
If you want to verify that an key is empty without create it, it'would be better to check with a itterator: map.find(KEY) != map.end()
If you want to create an empty row for a key in your map it would be like you specified it map[KEY]
That assertion will never fire given the two lines of code that you've shown.