源码实现:
// 类型特征运算模板
template<bool _Cond, typename _Iftrue, typename _Iffalse>
struct conditional
{ using type = _Iftrue; };
template<typename _Iftrue, typename _Iffalse>
struct conditional <false, _Iftrue, _Iffalse>
{ using type = _Iffalse; };
- 使用 SFINAE 技术, 先寻找 _Cond 为 false 的情况, 如果没找到, 回到主模板
- 此模板作用是进行选择判断, 是后续类的工具类
测试程序:
int main(){
std::cout << STL::conditional<true, STL::true_type, STL::false_type>::type::value
<< std::endl;
std::cout << STL::conditional<false, STL::true_type, STL::false_type>::type::value
<< std::endl;
}