如何传递std :: map作为c ++类函数中的默认构造函数参数 [英] How to pass std::map as a default constructor parameter in c++ class function

查看:1448
本文介绍了如何传递std :: map作为c ++类函数中的默认构造函数参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在尝试在Ubuntu 12.04上使用clang-3.3和clang-3.0中的 std :: map 时遇到问题:

I have a problem when attempting 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;
}

但是,虽然代码在 g ++ clang 我继续得到这些错误作为输出:

However, while the code compiles in both g++ and clang I keep getting 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.

我能找到的最匹配的问题是这个主题:如何将std :: map作为默认构造函数参数

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

但我不知道发生了什么问题。希望有人能对此有所了解。

But, I have no idea what's wrong. Hopefully, someone can shed some light on this, please.

更新:

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

是确定。感谢。

推荐答案

其他海报是正确的,我认为这是一个 Bug 13657 ,应在Clang 3.4中修复。

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

在错误报告和 C ++标准核心语言活动问题页面链接(和你的更新中提到的),你可以解决这个问题,通过添加括号到默认值如下:

As mentioned in the bug report and the C++ Standard Core Language Active Issues page linked from there (and as you mentioned in your update), you can work around the issue 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>()))

这篇关于如何传递std :: map作为c ++类函数中的默认构造函数参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆