How does std::map initialize its Data?

I noticed that std::map seems to know how to init it's data even when it is pretty complex.

For example, if there is a type containing an int, even when the default constructor does not initialize it, std::map will initialize it to 0.

How does it do this?
When an object is created a constructor is bound to run whether a default or user defined or any other and in that it will initialize the data.
Via default constructors.

Example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
template< typename T1, typename T2 >
struct my_pair {
   // default constructor runs default constructor for T1 and T2.
   // note the open-close parens.  This special syntax allows the
   // compiler to initialize POD-types (int, float, bool, etc) to 0 (false).
   my_pair() : 
       first(), second() {}

   my_pair( const T1& f, const T2& s ) :
       first( f ), second( s ) {}

   T1 first;
   T2 second;
};

my_pair<int, double> p1; // p1.first == 0, p1.second == 0.0
my_pair< std::string, std::vector<int> > p2;  // p2.first == empty string, p2.second == empty vector (0 elements) 


Topic archived. No new replies allowed.