How to pass std :: map as a default constructor parameter in a C ++ class function

I have a problem when trying to use std::map in clang-3.3 and clang-3.0 on Ubuntu 12.04:

 #include <iostream> #include <map> #include <string> class A { public: #if 0 //clang compiles ok typedef std::map<std::string,std::string> MapKeyValue_t; void PrintMap(const MapKeyValue_t &my_map = MapKeyValue_t()) #else // clang compiles fail void PrintMap(const std::map<std::string,std::string> &my_map = std::map<std::string,std::string>()) #endif { std::map<std::string,std::string>::const_iterator it; for (it = my_map.begin(); it != my_map.end(); it++) { std::cout << it->first << " " << it->second << std::endl; } } }; int main() { A a; a.PrintMap(); return 0; } 

However, although the code compiles in both g++ and clang , I always get these errors as output:

 test.cpp:14:36: error: expected ')' = std::map<std::string,std::string>()) ^ test.cpp:13:15: note: to match this '(' void PrintMap(const std::map<std::string,std::string> &my_map ^ test.cpp:14:24: error: expected '>' = std::map<std::string,std::string>()) ^ test.cpp:28:13: error: too few arguments to function call, expected 2, have 0 a.PrintMap(); ~~~~~~~~~~ ^ test.cpp:13:2: note: 'PrintMap' declared here void PrintMap(const std::map<std::string,std::string> &my_map ^ 3 errors generated. 

The closest I could find that matches my problem is this topic: How to pass std :: map as a default constructor parameter

But I do not know what happened. Hope someone can shed some light on this, please.

Update:

 void PrintMap(const std::map<std::string,std::string> &my_map = (std::map<std::string,std::string>())) 

OK. Thanks.

+6
source share
2 answers

I compiled and successfully executed it in vs2012.
Therefore, I think the problem is with compilers.

+1
source

Other posters are correct, I think this is an instance of Error 13657 , which should be fixed in Clang 3.4.

As mentioned in the bug report and C ++ Standard Core Language Active Issues , related from there (and, as you mentioned in your update), you can work around the problem by adding parentheses to the default value as follows:

 void PrintMap(const std::map<std::string,std::string> &my_map = (std::map<std::string,std::string>())) 
+1
source

Source: https://habr.com/ru/post/951451/


All Articles