#include <iostream>
using namespace std;
template<bool x>
struct bool_
{
static const bool value = x; //每個整型常量外覆器都包含一個value常量
typedef bool_<x> type; //無參元函數
typedef bool value_type; //value_type指明value的類型
operator bool() const //非常自然轉化一個值爲x的布爾值
{
return x;
}
};
int main()
{
cout << typeid(bool_<6>::type).name() << endl;
cout << typeid(bool_<6>::value_type).name() << endl;
cout << bool_<6>::value << endl;
cout << bool_<6>() << endl;
cout << typeid(bool_<false>::type).name() << endl;
cout << bool_<false>::value << endl;
cout << typeid(bool_<true>::type).name() << endl;
cout << bool_<true>::value << endl;
return 0;
}
結果: