欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++中std::conditional的使用說(shuō)明

 更新時(shí)間:2022年07月11日 14:46:40   作者:年年年年年  
這篇文章主要介紹了C++中std::conditional的使用說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

std::conditional的使用

今天在項(xiàng)目中發(fā)現(xiàn)C++11新特性中的std::conditional關(guān)鍵詞,經(jīng)過(guò)查詢(xún)資料,學(xué)習(xí)到了其中含義。

具體用法

std::conditional<表達(dá)式, 類(lèi)型1, 類(lèi)型2>

具體理解為

如果表達(dá)式為真則定義的變量為類(lèi)型1,如果表達(dá)式為假則定義的變量為類(lèi)型2。

如:

typedef typename std::conditional<true, int, double>::type Type1;// => int Type1

則Type1的類(lèi)型為int類(lèi)型

以下顯示了更多的例子

#include <iostream>
#include <type_traits>
#include <typeinfo>
 
int main() 
{
    typedef typename std::conditional<true, int, double>::type Type1;
    typedef typename std::conditional<false, int, double>::type Type2;
     typedef typename std::conditional<sizeof(int) == sizeof(double), int, double>::type Type3;
 
    std::cout << typeid(Type1).name() << std::endl; // 輸出:i (代表int類(lèi)型)
    std::cout << typeid(Type2).name() << std::endl; // 輸出:d (代表double類(lèi)型)
    std::cout << typeid(Type3).name() << std::endl; // 輸出:d (代表double類(lèi)型)
    
    Type1 a = 3.1; // 由于Type1的類(lèi)型為int所以3.1被強(qiáng)制轉(zhuǎn)換為3
    Type2 b = 4.2; // Type2的類(lèi)型為double,4.2保存在變量b中
    std::cout << a +  b << std::endl; // 3+4.2,最終輸出為7.2
}

利用std::conditional實(shí)現(xiàn)變量的多類(lèi)型

//std::conditional<bool, A, B>::type 
const bool kEnableOffsetRender = false; 
class LoginDialog : public std::conditional<kEnableOffsetRender, ui::WindowImplBase, nim_comp::ShadowWndBase>::type
{
public:
    //todo
};
 
/*
    if(kEnableOffsetRender)
    {
        type = ui::WindowImpBase;
    }
    else
    {
        type = nim_comp::ShadowWndBase;
    }
*/

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論