There is such code:

std::map<int, int> m; int a = m[0]; 

Is there a guarantee that a will contain 0? Or is there a chance that there will be garbage?

The general question is: is initialization of simple types (char, int, float) performed while reading a nonexistent map element? Or will there be garbage, as it happens with variable declarations without initialization, and you need to always initialize map elements manually?

    2 answers 2

    Yes, there is a guarantee. In the "classic" C ++ 98, the behavior of such an operator[] with the new key x described as the insertion of the make_pair(x, T()) element, where T is the data type of the map. In your case, this is an int and the expression int() generates a null value.

    Starting with C ++ 11, the operator[] specification is more "tangled". The pair is constructed using piecewise_construct with an empty argument list for the second element. For the int type, such initialization results in zeroing.

      Yes, it guarantees. Initialization is performed by the value (it seems so it is called).

      "Standard Library C ++" by N. Josattis, second edition, p. 378:

      "If the key is an index for which there is no element, a new element is automatically inserted into the display. The value of the new element is initialized by the default constructor for the corresponding type. ... all elementary types have default constructors that initialize their values ​​to zero."

      From the standard :

      T& operator[](const key_type& x);
      1 Effects: If there is no key in the map, inserts value_type(x, T()) into the map.

      And about the initialization of the value from the same:

      8 To value-initialize an object of type T means:
      (8.1) - if T is a (possibly cv-qualified) class type (Clause 9) without a default constructor (12.1) or a default constructor;
      (8.2) - if it is a constructor, it can be used, then it will be checked. -trivial default constructor, the object is default-initialized;
      (8.3) - if T is an array type, then each element is value-initialized;
      (8.4) - otherwise, the object is zero-initialized.

      • all elementary types have default constructors that initialize their values ​​with zeros - Bah-ty-shki, a new word in C ++! - gbg
      • @gbg: Yes, this statement is formally incorrect. Of course, the int type has no constructor. But as a simplification, it is also found in Straustrup's TC ++ PL. This is something like a "forgivable deception", made to simplify the presentation for beginners. - AnT
      • @AnT - and how then to conduct an interview? How to prove a juna, whose program is segolfeted because int is not initialized, if the smart Stroustrup has allowed itself such a brilliant deception in its textbook? - gbg
      • @gbg: I don’t remember all the details of the “alternative system” built by Stroustrup. It seems to be written to him that scalar types have constructors, but they must always be "called explicitly" or something like that ... That is, int() and int i{}; - These are guaranteed zeros, but int i; nothing guarantees. It is clear that this "alternative system" contradicts the standard, but I do not feel like throwing out the full and sophisticated initialization rule system C ++ right away on the newbie as well. - AnT
      • @gdb You know, I don't even want to argue. You can assume that "there is no guarantee and there will be garbage" - such an assumption will complicate the program a little, but it will not spoil it at all :) And yet, tell interest for - what will return the call ret<int>() for template<class T> T ret() { return T(); } template<class T> T ret() { return T(); } - rubbish? - Harry