c++ - "C2593: operator = is ambiguous" when populating std::map -
c++ - "C2593: operator = is ambiguous" when populating std::map -
i have std::map
i'm trying initialize initialization list. in 2 places, in 2 different ways. first 1 works, while other 1 causes error mentioned in title.
here's 1 works:
void foo() { static std::map<std::string, std::string> foomap = { { "first", "abc" }, { "second", "def" } }; }
while 1 not:
class bar { public: bar(); private: std::map<std::string, std::string> barmap; }; bar::bar() { barmap = { // <-- error line { "first", "abc" }, { "second", "def" } }; }
why error when trying initialize class member, while static map works? @ moment, can populate fellow member first creating local variable , swapping fellow member this:
bar::bar() { std::map<std::string, std::string> tmpmap = { { "first", "abc" }, { "second", "def" } }; barmap.swap(tmpmap); }
however, feels rather counter-intuitive compared populating fellow member directly.
edit: here's compiler output.
this bug in compilers overload resolution mechanism - or standard library implementation. overload resolution states in [over.ics.rank]/3 that
— list-initialization sequence l1
improve conversion sequence list-initialization sequence l2
if l1
converts std::initializer_list<x>
x
, l2
not.
here, x
std::pair<std::string, std::string>
. l1
converts list parameter of
map& operator=( std::initializer_list<value_type> ilist );
whilst l2
converts list 1 of next functions' parameters:
map& operator=( map&& other ); map& operator=( const map& other );
which aren't initializer_list
s. seek use
barmap = decltype(barmap){ { "first", "abc" }, { "second", "def" } };
which should select move-assignment operator (demo vc++). temporary should optimized away according re-create elision.
c++ c++11
Comments
Post a Comment