“C2593:operator = is ambiguous”当填充std :: map [英] "C2593: operator = is ambiguous" when populating 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);
}
然而,与直接填充成员相比,
这是您的编译器重载解析机制或其标准库实现中的错误。
重载解决方案在[over.ics.rank] / 3中清楚地说明
- 列表初始化序列
c>是L1
是比
更好的转换序列list-initialization sequenceL2 if
L1
为某些X
和<$ c $转换为
std :: initializer_list< X>
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 sequenceL2
ifL1
converts tostd::initializer_list<X>
for someX
andL2
does not.Here,
X
isstd::pair<std::string, std::string>
.L1
converts your list to the parameter ofmap& 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_list
s.
You could try to usebarMap = 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屋!