Braced initialisation of std::map member compiler error

爷,独闯天下 提交于 2019-12-14 02:32:32

问题


The following code does not compile using Visual Studio 2013. It does compile using Xcode 6.1 (Clang 3.5).

std::string s1("one");
std::string s2("two");
std::string s3("three");
std::string s4("four");

class X
{
    typedef std::map<std::string, std::string> MyMapType;
    MyMapType map1 = { { s1, s2 }, { s3, s4 } };
    MyMapType map2 = { { std::make_pair(s1, s2) }, { std::make_pair(s3, s4) } };
};

The error reported for both declarations is:

error C2664: 'std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>::map(std::initializer_list<std::pair<const _Kty,_Ty>>,const std::less<_Ty> &,const std::allocator<std::pair<const _Kty,_Ty>> &)' : cannot convert argument 2 from 'initializer-list' to 'const std::allocator<std::pair<const _Kty,_Ty>> &'

However, the following does compile:

int main()
{
    typedef std::map<std::string, std::string> MyMapType;
    MyMapType map3 = { { s1, s2 }, { s3, s4 } };

    return 0;
}

Please can someone explain this.


回答1:


Visual C++ 2013 is known to be buggy with its handling of list initialization in non-static data member initializers and constructor member initializer lists. It was so badly broken - in some cases causing silent bad codegen - that they simply made it a compiler error in all cases in Visual Studio 2013 Update 3 (shipping an actual fix was apparently deemed too risky for an update).

Your code compiles fine with Microsoft's online compiler, which runs a preview version of Visual C++ 2015, so it looks like this has been fixed.

A workaround (noted in the MSDN page linked above) is explicitly specifying the type on the RHS as well, so that you are not actually list-initializing the non-static data member.

MyMapType map1 = MyMapType{ { s1, s2 }, { s3, s4 } };
MyMapType map2 = MyMapType{ { std::make_pair(s1, s2) }, { std::make_pair(s3, s4) } };


来源:https://stackoverflow.com/questions/30062224/braced-initialisation-of-stdmap-member-compiler-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!