Robert Goulet
Robert Goulet

Reputation: 61

Static initialization of a map with a pair

I'm trying to statically initialize a map that contains a pair:

typedef map<int, pair<int, int>> mytype;
static const mytype mymap = { 3, {3, 0} };

I'm using Visual Studio 2013, but I get the error:

 error C2440: 'initializing' : cannot convert from 'initializer-list' to 'std::map<int,std::pair<int,int>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'

Any idea what would cause this? I thought VS2013 had this C++11 functionality.

Upvotes: 4

Views: 1832

Answers (2)

eerorika
eerorika

Reputation: 238471

You're missing one set of braces:

static const mytype mymap = { { 3, {3, 0} } };
                            ^ ^    ^ 
                            | |    pair<int,int> (value)
                            | pair<const key, value> (map element)
                            map<key, value>

Upvotes: 14

Christian Hackl
Christian Hackl

Reputation: 27548

The compiler thinks you want to initialize the map with two elements.

The correct syntax would be:

static const mytype mymap = { { 3, {3, 0} } };

Upvotes: 1

Related Questions