map<int,int> default values
As soon as you access the map with the [] operator, if the key doesn't exist it gets added. The default initializer of the int type gets invoked - so it will get a value of 0.
Yes, it is safe to assume.
The map's operator[]
is specified thus:([map.access])
Effects: If there is no key equivalent to
x
in the map, insertsvalue_type(std::move(x), T())
into the map.
Returns: A reference to themapped_type
corresponding tox
in*this
.
T()
uses value-initialisation for all T
except void
([expr.type.conv]/2), and value-initialisation for a primitive results in zero-initialization ([dcl.init]/7).
Therefore, the expression evaluates to a reference to an object with value zero ([dcl.init]/5).
The operator++
call then increments that object to one, and evaluates to one.
(All references are C++11.)
Yes, the default value will be the default of that type. If you want another default, you can create a class that behaves like an int but has a different default constructor.