[cfe-dev] how to pass stdmap as a default constructor parameter

Jean Lee xiaoyur347 at gmail.com
Sat Aug 10 02:17:02 PDT 2013


I suffer a problem when using std::map in clang. I compile it in clang 3.3
under ubuntu 12.04, and the problem is also in clang 3.0.
The codes like follows (also in attachment):
================================================
#include <iostream>
#include <map>
#include <string>

class A
{
public:
#if 0
typedef std::map<std::string,std::string> MapKeyValue_t;
void PrintMap(const MapKeyValue_t &my_map
= MapKeyValue_t())
#else
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;
}
====================================================
The output is :
test.cpp:9:36: error: expected ')'
        = std::map<std::string,std::string>())
                                          ^
test.cpp:8:14: note: to match this '('
void PrintMap(const std::map<std::string,std::string> &my_map
             ^
test.cpp:9:24: error: expected '>'
        = std::map<std::string,std::string>())
                              ^
test.cpp:22:13: error: too few arguments to function call, expected 2, have
0
        a.PrintMap();
        ~~~~~~~~~~ ^
test.cpp:8:1: note: 'PrintMap' declared here
void PrintMap(const std::map<std::string,std::string> &my_map
^
3 errors generated.

=========================================================
It compiles ok if I write this way:
typedef std::map<std::string, std::string> MapKeyValue_t;
void PrintMap(const MapKeyValue_t &my_map
= MapKeyValue_t())

Both compile ok under g++.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130810/53cb568c/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: test.cpp
Type: text/x-c++src
Size: 546 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20130810/53cb568c/attachment.cpp>


More information about the cfe-dev mailing list