[英]Is it a non-standard std::pair implementation, a compiler bug, or a non-standard code?
以下代码在VS2005和gcc-4.3.4上编译。
#include <algorithm>
#include <iostream>
template<class C, class T, T C::*x>
struct X { };
typedef std::pair<int,int> Pr;
X<Pr, int, &Pr::first> var;
int main()
{
std::cout << "hello\n";
}
但它无法在VS2010上编译,并显示错误消息:
1>d:\a\testvs10\testvs10.cpp(13): error C2440: 'specialization' : cannot convert from 'int std::_Pair_base<_Ty1,_Ty2>::* ' to 'int std::pair<_Ty1,_Ty2>::* '
1> with
1> [
1> _Ty1=int,
1> _Ty2=int
1> ]
1> Standard conversion from pointer-to-member of base to pointer-to-member of derived is not applied for template arguments
据我所知,在microsoft的VS2010实现中, Pr::first
实际上是_Pair_base
的成员。 但是,AFAIK,没关系。 &Pr::first
仍然必须是int Pr::*
类型。 请注意,以下代码编译正常:
int Pr::* x = &Pr::first;
那么,它是非标准的std :: pair实现,编译器错误还是非标准代码?
该标准提供了对一对的清晰定义,在C ++ 03和C ++ 11中,该对包含两个成员属性,并提供了对的外观的精确代码。 我认为这种行为(将成员移动到基类)作为违反合同的行为,因为您的代码根据标准有效,并且被实施拒绝。
因此,VC2010的实现并不符合这一特定情况。
标准在匹配模板参数时不允许任何转换(查找标准段落,将编辑)。
typedef std::pair<int,int> Pr;
X<Pr, int, &Pr::first> var;
这将X
as(为了简洁而省略了std::
:)
X<pair<int,int>, int, int pair<int,int>::*>
但是&Pr::first
的静态类型是
int _Pair_base<int,int>::*
因此编译器需要执行错误中显示的转换。 我不知道gcc如何实现std::pair
,但似乎没有继承。 我会查找并再次编辑。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.