一段bool外覆器的代碼測試

#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;
}


結果:

發佈了188 篇原創文章 · 獲贊 43 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章