Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::map default value for build-in type

Recently, I was confused by the std::map operator[] function. In the MSDN library, it says: "If the argument key value is not found, then it is inserted along with the default value of the data type." I tryed to search much more exactly explanation for this issue. For example here: std::map default value In this page, Michael Anderson said that "the default value is constructed by the default constructor(zero parameter constructor)".

Now my quest comes to this:"what the default value for the build-in type?". Was it compiler related? Or is there a standard for this issue by the c++ stardard committee?

I did a test on visual studio 2008 for the "int" type, and found the "int" type is construted with the value 0.

like image 779
WKPlus Avatar asked Dec 24 '10 03:12

WKPlus


People also ask

What is the default value of map in C++?

A map is a container which is used to store a key-value pair. By default, In Primitive datatypes such as int, char, bool, float in C/C++ are undefined if variables are not initialized, But a Map is initially empty when it is declared.

How do you initialize a map to 0?

What exactly do you want to initialize to zero? map's default constructor creates empty map. You may increase map's size only by inserting elements (like m["str1"]=0 or m. insert(std::map<std::string,int>::value_type("str2",0)) ).

Is map ordered by default?

By default, a Map in C++ is sorted in increasing order based on its key.


2 Answers

This is defined in the standard, yes. map is performing "default initialization" in this case. As you say, for class types, that calls a no-arguments constructor.

For built-in types, in the '98 standard, see section 8.5, "Initializers":

To default-initialize an object of type T means:

  • if T is a non-POD ...
  • if T is an array type ...
  • otherwise, the storage for the object is zero-initialized

And, previously,

To zero-initialize storage for an object of type T means:

  • if T is a scalar type, the storage is set to the value 0 (zero) converted to T

Scalar types are:

  • Arithmetic types (integer, floating point)
  • Enumeration types
  • Pointer types
  • Pointer to member types

In particular, the behaviour you see with an integer (initialized to zero) is defined by the standard, and you can rely on it.

like image 108
Luke Halliwell Avatar answered Sep 20 '22 11:09

Luke Halliwell


The C++11 standard still requires that std::map zero-initializes built in types (as did the previous standard), but the reasons are a bit different to those in Luke Halliwell's answer. In particular, to 'default-initialize' a built-in data type doesn't mean zero-initialize in the C++11 standard, but rather it would mean 'do nothing'. What actually happens in std::map::operator[] is a 'value-initialization'.

Nevertheless, the end result in the new standard is the same as in Luke's answer. The values will be zero-initialized. Here are the relevant parts of the standard:

Section 23.4.4.3 "map element access" says

T& operator[](const key_type& x);

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

...

The expression T() is described in section 8.5

An object whose initializer is an empty set of parentheses, i.e., (), shall be value-initialized.

X a();

And this kind of 'value-initialization' is described in the same section

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1), then the default constructor for T is called (and the initialization is ill-formed if T 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, if T’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.
like image 44
karadoc Avatar answered Sep 24 '22 11:09

karadoc