Why can not omit braces when initializing map?

Viewed 5781

Inspired by this answer, I tried next example :

#include <map>
#include <string>
#include <iostream>

int main()
{
  const std::map< int, std::string > mapping = {
      1, "ONE",
      2, "TWO",
    };

  const auto it = mapping.find( 1 );
  if ( mapping.end() != it )
  {
    std::cout << it->second << std::endl;
  }
  else
  {
    std::cout << "not found!" << std::endl;
  }
}

and the compilation failed with next error message (g++ 4.6.1) :

gh.cpp:11:5: error: could not convert '{1, "ONE", 2, "TWO"}' from '<brace-enclosed initializer list>' to 'const std::map<int, std::basic_string<char> >'

I know how to fix it :

  const std::map< int, std::string > mapping = {
      {1, "ONE"},
      {2, "TWO"},
    };

but why the compilation fails in the top example?

3 Answers
Related