【发布时间】:2016-08-17 21:00:46
【问题描述】:
我尝试理解enable_if 实现,它是一对模板类。我不明白,为什么enable_if<true, int> 与第一个不匹配?这是怎么决定的?
#include <iostream>
template <bool, class T = void>
struct enable_if
{
enable_if() { std::cout << "invalid type";}
};
template <class T>
struct enable_if<true, T>
{
typedef T type;
enable_if() { std::cout <<"valid type";}
};
int main(){
enable_if<0==0, int> example; // print "valid type"
return 0;
}
【问题讨论】:
-
最适合和其他东西。如果可以选择显式特化,则可以,否则默认为基本模板。
标签: c++ templates c++11 template-classes