亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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ò)查詢資料,學(xué)習(xí)到了其中含義。

具體用法

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

具體理解為

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

如:

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

則Type1的類型為int類型

以下顯示了更多的例子

#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類型)
    std::cout << typeid(Type2).name() << std::endl; // 輸出:d (代表double類型)
    std::cout << typeid(Type3).name() << std::endl; // 輸出:d (代表double類型)
    
    Type1 a = 3.1; // 由于Type1的類型為int所以3.1被強(qiáng)制轉(zhuǎn)換為3
    Type2 b = 4.2; // Type2的類型為double,4.2保存在變量b中
    std::cout << a +  b << std::endl; // 3+4.2,最終輸出為7.2
}

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

//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)論