支持std :: map成员编译器初始化 [英] Braced initialisation of std::map member compiler error
问题描述
以下代码不使用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)}};
};
报告的两个声明的错误是:
错误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>>&)':不能将参数2从'initializer-list'转换为'const std :: allocator< std :: pair< const _Kty,_Ty& &'
但是,以下操作会编译:
$ b b
int main()
{
typedef std :: map< std :: string,std :: string> MyMapType;
MyMapType map3 = {{s1,s2},{s3,s4}};
return 0;
}
请有人解释这一点。
Studio 2013 Update 3中的所有情况下的编译器错误(运送实际修订显然被认为对于更新而言风险太大)。
您的代码可以通过 Microsoft的在线编译器进行编译,运行Visual C ++ 2015的预览版本,所以看起来这个问题已修复。
一个解决方法(在上面链接的MSDN页面中注明)也明确指定了RHS上的类型,不实际列表初始化非静态数据成员。
MyMapType map1 = MyMapType {{s1,s2},{s3, s4}};
MyMapType map2 = MyMapType {{std :: make_pair(s1,s2)},{std :: make_pair(s3,s4)}};
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.
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) } };
这篇关于支持std :: map成员编译器初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!