Braced initialisation of std::map member compiler

2019-08-04 01:45发布

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条回答
SAY GOODBYE
2楼-- · 2019-08-04 02:43

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) } };
查看更多
登录 后发表回答