Mam problem podczas próby użycia std::map
w brzękiem-3.3 i dzyń-3.0 na Ubuntu 12.04:Jak przekazać std :: map jako parametru domyślnego konstruktora w C++ funkcją klasy
#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;
}
Jednakże, podczas gdy kod kompiluje zarówno g++
i clang
ciśgle te błędy jako wyjście:
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.
najbliższy rzeczą mogłyby znaleźć pasujący mój problem polega na ten temat: How to pass std::map as a default constructor parameter
Ale nie mam pojęcia, co jest nie tak. Mam nadzieję, że ktoś może rzucić trochę światła na ten temat.
Aktualizacja:
void PrintMap(const std::map<std::string,std::string> &my_map
= (std::map<std::string,std::string>()))
jest w porządku. Dzięki.
Looks jak http://llvm.org/bugs/show_bug.cgi?id=13657 - sugerowane obejście polega na umieszczeniu parensów wokół domyślnego argumentu, co powoduje, że jest on tutaj kompilowany. – Mat