Unordered_map - {{key, value}, {key, value}} syntax is invalid

I am trying to compile the code taken here

// constructing unordered_maps #include <iostream> #include <string> #include <unordered_map> typedef std::unordered_map<std::string,std::string> stringmap; stringmap merge (stringmap a,stringmap b) { stringmap temp(a); temp.insert(b.begin(),b.end()); return temp; } int main () { stringmap first; // empty stringmap second ( {{"apple","red"},{"lemon","yellow"}} ); // init list stringmap third ( {{"orange","orange"},{"strawberry","red"}} ); // init list stringmap fourth (second); // copy stringmap fifth (merge(third,fourth)); // move stringmap sixth (fifth.begin(),fifth.end()); // range std::cout << "sixth contains:"; for (auto& x: sixth) std::cout << " " << x.first << ":" << x.second; std::cout << std::endl; return 0; } 

with MSVC2012 but I get

error C2143: syntax error: missing ')' before '{'

on the code line

 stringmap second ( {{"apple","red"},{"lemon","yellow"}} ); // init list 

Did I miss something?

+6
source share
3 answers

Visual Studio 2012 lacks many modern C ++ functions, including initialiser lists . See here for a review.

+5
source

There is nothing wrong with the code, and it compiles fine with GCC and Clang. The problem is with Visual C ++.

Initializer lists are one of the features that will be available in Visual Studio 2012 Update 2 . This means that you cannot use this feature in Visual Studio 2012. There is a series of Community Tech Previews (CTP) , but they come with some minor issues, including the lack of IntelliSense support and very clear disclaimers saying that they are not Designed for use in production code.

So, in short: your code is correct, but it will not compile in VS2012 until Microsoft releases the Visual Studio 2012 2 update. There is no information when this will happen, but Visual Studio 2012 was first released in August 2012 and the last The update (Update 1) was released in November 2012. Since then there has been little news, but it has been "soon" since the end of last year.

Update Now Update 2 has been released. However, it does not include any of the promised C ++ enhancements from CTP Update 2. This is ridiculous given that they should be previews of what should appear in Update 2. Apparently, the Visual C ++ team "is currently finalizing release plans for of these functions, "and" will tell you more soon. " (from comments Update 2 release .

+4
source

To be more precise, initializer lists are presented in CTP VS2012, but this update has not yet been published and does not contain support for initializer lists in the standard library - IOW, they are close, but Microsoft has not finally finished them.

+2
source

All Articles