Are new std::map elements initialized?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • int2str@gmail.com

    #1

    Are new std::map elements initialized?

    Hi!

    Given the following code:

    #include <map>
    #include <string>
    #include <cassert>

    int main()
    {
    std::map< std::string, unsigned > my_map;
    ++my_map["Test"];

    assert( my_map["Test"] == 1 );
    }

    Can I rely on the fact that the newly created map element with key
    "Test" will be created with a value of 0?

    Josuttis's "The C++ Standard Library" seems to indicate so (pg. 207).

    The Standard briefly states (23.3.1.2 para 1):

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

    Returns: (*((insert(make _pair(x, T()))).first)). second

    So it looks like the default constructor for T is called. For PODs like
    "unsigned", does the default constructor (?) guarantee
    0-initialization?

    Thanks for your insights!

    Cheers,
    Andre

  • Gregory

    #2
    Re: Are new std::map elements initialized?

    As I know there is no such thing as default constructor for C/C++ basic
    types.

    Try

    void foo()
    {
    int i, j; // no default constructor
    }

    Neither i nor j are assigned 0. They both are uninitialized (junk).

    Gregory

    Comment

    • red floyd

      #3
      Re: Are new std::map elements initialized?

      Gregory wrote:[color=blue]
      > As I know there is no such thing as default constructor for C/C++ basic
      > types.
      >
      > Try
      >
      > void foo()
      > {
      > int i, j; // no default constructor
      > }
      >
      > Neither i nor j are assigned 0. They both are uninitialized (junk).
      >
      > Gregory[/color]

      but i = int() does initialize to 0.
      So, If a map uses:
      return (*((insert(make _pair(x, T()))).first)). second
      as Josuttis claims, then if T is int, then the make_pair() call with
      int() as the second parameter creates a pair with 0 as the second element.


      Comment

      • Gregory

        #4
        Re: Are new std::map elements initialized?

        Yes, you are right. I wrote a test and my_map["Test"] returned 0.
        I also checked g++ STL implementation we have and indeed
        map::operator[] calls integer constructor - int() as you said.

        _Tp& operator[](const key_type& __k) {
        iterator __i = lower_bound(__k );
        // __i->first is greater than or equivalent to __k.
        if (__i == end() || key_comp()(__k, (*__i).first))
        __i = insert(__i, value_type(__k, _Tp()));
        return (*__i).second;
        }


        Gregory

        Comment

        • Jonathan Mcdougall

          #5
          Re: Are new std::map elements initialized?

          int2str@gmail.c om wrote:[color=blue]
          > Hi!
          >
          > Given the following code:
          >
          > #include <map>
          > #include <string>
          > #include <cassert>
          >
          > int main()
          > {
          > std::map< std::string, unsigned > my_map;
          > ++my_map["Test"];
          >
          > assert( my_map["Test"] == 1 );
          > }
          >
          > Can I rely on the fact that the newly created map element with key
          > "Test" will be created with a value of 0?[/color]

          Yes. std::map::opera tor[] returns (*((insert(make _pair(x,
          T()))).first)). second and T() default-initializes an object. For
          non-PODs, the default ctor is called; for arrays, each element is
          default-initialized; other types are zero-initialized.


          Jonathan


          Jonathan

          Comment

          Working...