以下代码无法使用 Visual Studio 2013 进行编译。它使用 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) } };
};
两个声明报告的错误是:
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>> &'
但是,以下内容会编译:
int main()
{
typedef std::map<std::string, std::string> MyMapType;
MyMapType map3 = { { s1, s2 }, { s3, s4 } };
return 0;
}
请有人可以解释一下。