STL杂记(2)

 SFINAE

This "substitution-failure-is-not-an-error" (SFINAE) principle is clearly an important ingredient to make the overloading of function templates practical.

typedef char RT1;
typedef struct { char a[2]; } RT2;

template<typename T> RT1 test(typename T::X const*);
template<typename T> RT2 test(...);

class _Cls{

public

  typedef char X; //traits

};

test<_Cls>(0);

#define type_has_member_type_X(T) /
        (sizeof(test<T>(0)) == 1)

 

 (type_has_member_type_X(_Cls))
  ? std::cout << "YES/n"
  : std::cout << "NO/n";
这里会输出YES,说明_Cls是符合“一个有内部类型是X的类型”要求的类型。

(用处:能够判断一个类型是否是符合某一“特性”的类型,发挥这一作用的关键是找到这个“特性”)

 

To allow overload resolution of function templates using type traits rather than the type itself.

 

This is an acronym that means "substitution-failure-is-not-an-error" and it is used in the context of template functions overloading. When the compiler evaluates each overloaded template functions, it will not emit an error if by using the template parameter on one of the potential candidate function would generate an error. The compiler will just discard that function from the list of potential candidates.

 

老外说的

http://www.martinecker.com/wiki/index.php?title=SFINAE_Principle

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值