Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unordered_map - {{key,value},{key,value}} syntax invalid

I'm trying to compile the code taken from 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'm receiving

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

on the code line

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

Am I missing something?

like image 593
Johnny Pauling Avatar asked Apr 02 '13 14:04

Johnny Pauling


3 Answers

Visual Studio 2012 lacks many of the modern C++-features, among them are initialiser lists. See here for an overview.

like image 128
bash.d Avatar answered Nov 13 '22 01:11

bash.d


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

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

So, in short: your code is correct, but it won't compile in VS2012 until Microsoft releases Visual Studio 2012 Update 2. There is no telling when this will come, but Visual Studio 2012 was first released in August 2012 and the last update (Update 1) was released in November 2012. Since then, there's been little news on the subject, but it's been "coming soon" since the end of last year.

Update Now, Update 2 has been released. However, it does not include any of the promised C++ improvements from the Update 2 CTPs. This is funny, considering that they are supposed to be previews of what is to come in Update 2. Apparently, the Visual C++ team is "currently finalizing the release plans for those features" and "will share more details soon". (from the comments on the Update 2 release announcement.)

like image 44
Agentlien Avatar answered Nov 13 '22 01:11

Agentlien


To be more accurate, initializer lists are featured in the VS2012 CTP, but that update is not yet published, and does not contain support for initializer lists in the Standard library- IOW, they're close, but Microsoft haven't quite finished them yet.

like image 21
Puppy Avatar answered Nov 13 '22 01:11

Puppy