“C2593:operator = is ambiguous”当填充std :: map [英] "C2593: operator = is ambiguous" when populating std::map

查看:221
本文介绍了“C2593:operator = is ambiguous”当填充std :: map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 std :: map 我试图初始化初始化列表。我在两个地方做这个,有两种不同的方式。



这是一个工作原理:

  void foo(){
static std :: map< std :: string,std :: string> fooMap =
{
{First,ABC},
{Second,DEF}
};
}

虽然这不是:

  class Bar {
public:
Bar();
private:
std :: map< std :: string,std :: string> barMap;
};

Bar :: Bar(){
barMap = {//< - 这是错误行
{First,ABC},
{Second,DEF}
};
}

为什么在尝试初始化类成员时会出现错误,静态地图工程?现在,我可以通过首先创建一个局部变量,然后与成员交换它来填充成员,例如:

  :: Bar(){
std :: map< std :: string,std :: string> tmpMap = {
{First,ABC},
{Second,DEF}
};

barMap.swap(tmpMap);
}

然而,与直接填充成员相比,






编辑:这是编译器输出。 / a>

这是您的编译器重载解析机制或其标准库实现中的错误。
重载解决方案在[over.ics.rank] / 3中清楚地说明


- 列表初始化序列 L1 是比
更好的转换序列list-initialization sequence L2 if L1 为某些 X 和<$ c $转换为
std :: initializer_list< X>

c>是 std :: pair< std :: string,std :: string> L1 将您的列表转换为

的参数

  operator =(std :: initializer_list< value_type> ilist); 

L2 将列表转换为以下函数的参数:

  map& operator =(map&& other); 
地图& operator =(const map& other);

这显然不是 initializer_list



您可以尝试使用

  barMap = decltype(barMap){
{First,ABC},
{Second,DEF}
};

其中应该选择移动赋值运算符(用VC ++演示)。临时文件也应根据复制精度进行优化。


I have a std::map I'm trying to initialize with an initialization list. I do this in two places, in two different ways. The first one works, while the other one causes the error mentioned in the title.

Here's the one that works:

void foo() {
    static std::map<std::string, std::string> fooMap =
    {
        { "First", "ABC" },
        { "Second", "DEF" }
    };
}

While this one does not:

class Bar {
    public:
        Bar();
    private:
        std::map<std::string, std::string> barMap;
};

Bar::Bar() {
    barMap = { // <-- this is the error line
        { "First", "ABC" },
        { "Second", "DEF" }
    };
}

Why do I get the error when trying to initialize the class member, while the static map works? At the moment, I can populate the member by first creating a local variable and then swapping it with the member like this:

Bar::Bar() {
    std::map<std::string, std::string> tmpMap = {
        { "First", "ABC" },
        { "Second", "DEF" }
    };

    barMap.swap(tmpMap);
}

However, this feels rather counter-intuitive compared to just populating the member directly.


EDIT: Here's the compiler output.

解决方案

This is a bug in your compilers overload resolution mechanism - or its standard library implementation. Overload resolution clearly states in [over.ics.rank]/3 that

— List-initialization sequence L1 is a better conversion sequence than list-initialization sequence L2 if L1 converts to std::initializer_list<X> for some X and L2 does not.

Here, X is std::pair<std::string, std::string>. L1 converts your list to the parameter of

map& operator=( std::initializer_list<value_type> ilist );

Whilst L2 converts the list to one of the following functions' parameters:

map& operator=( map&& other );
map& operator=( const map& other );

Which clearly aren't initializer_lists.


You could try to use

barMap = decltype(barMap){
    { "First", "ABC" },
    { "Second", "DEF" }
};

Which should select the move-assignment operator (Demo with VC++). The temporary should also be optimized away according to copy elision.

这篇关于“C2593:operator = is ambiguous”当填充std :: map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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