Kie*_*iel 3 c++ templates type-traits c++11
这是我编写的用于学习C++的简单模板编程:
#include <type_traits>
#include <iostream>
using namespace std;
template<typename T>
T foo(T t, true_type)
{
cout << t << " is integral! ";
return 2 * t;
}
template<typename T>
T foo(T t, false_type)
{
cout << t << " ain't integral! ";
return -1 * (int)t;
}
template<typename T>
T do_foo(T t){
return foo(t, is_integral<T>());
}
int main()
{
cout << do_foo<int>(3) << endl;
cout << do_foo<float>(2.5) << endl;
}
Run Code Online (Sandbox Code Playgroud)
它没有任何想象力,但它确实编译和工作.
我想知道该部分是如何is_integral<T>()工作的?
我正在读这个:http://en.cppreference.com/w/cpp/types/is_integral我找不到这种行为的具体描述 - 没有定义operator()
is_integral<T>是一种继承自true_type或的类型false_type.
is_integral<T>()是构造函数调用,因此其中一个类型的实例是调用的参数foo.然后根据它是哪一个来选择过载.
| 归档时间: |
|
| 查看次数: |
839 次 |
| 最近记录: |