網(wǎng)站首頁 編程語言 正文
std::conditional的使用
今天在項(xiàng)目中發(fā)現(xiàn)C++11新特性中的std::conditional關(guān)鍵詞,經(jīng)過查詢資料,學(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;
}
*/
原文鏈接:https://blog.csdn.net/weixin_42142630/article/details/121361497
相關(guān)推薦
- 2022-12-25 Android?Flutter實(shí)現(xiàn)評分組件的示例代碼_Android
- 2022-08-21 caffe的python接口caffemodel參數(shù)及特征抽取示例_python
- 2023-03-21 C#實(shí)體類轉(zhuǎn)換的兩種方式小結(jié)_C#教程
- 2022-04-09 SpringBoot 項(xiàng)目打包成jar包,并執(zhí)行Jar文件
- 2023-05-16 Golang的鎖機(jī)制使用及說明_Golang
- 2022-04-17 Bootstrap typeahead自動補(bǔ)全插件的坑
- 2022-10-22 SQL?Server實(shí)現(xiàn)group_concat功能的詳細(xì)實(shí)例_MsSql
- 2022-08-15 spring5:創(chuàng)建spring項(xiàng)目并引入jar包
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支