外覆器:跨越编译期和运行期的边界。
#include <iostream>
using namespace std;
template<bool>
struct select_impl
{
template<typename T>
static void print(T)
{
cout << T::value << endl;
}
};
template<>
struct select_impl<false>
{
template<typename T>
static void print(T x)
{
cout << x << endl;
}
};
template<typename T>
void f(T x)
{
select_impl<is_class<T>::value>::print(x);
}
struct TestClass
{
static const int value{ 100 };
};
int main(int argc, char* argv[])
{
f(20);
f(TestClass());
return 0;
}