C++标准库学习之 decltype 类型指示符 typeid 运算符

先来说一下 decltype 的这个概念, decltype 是一个推到类型,在C++ 中的推到类型常常使用 auto ,但是 auto 的使用存在一些限制

举个例子

arduino 复制代码
template<class T>
auto getT(T t)-> decltype (t){
    return t;
}

在上面这段代码中,使用了模板类型,你传入的类型是什么,我就给你返回什么类型, 如果不使用 decltype 约束返回类型是 t 的类型,那么这段代码就无解了,

decltype 获取的常常是变量的左值,也就是变量的类型 ,配合 typeid 可以获取 左值类型的id

arduino 复制代码
#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

template<class  T>
auto get_decltype(T t) -> decltype(t) {
    return t;
}




int main(){
    
    
    decltype(get_decltype(10)) tsm_int =10;
    
    // typeid 返回类型是一个 typeinfo ,他在 <typeinfo> 这个包下
    cout<< "tsm 实际的类型是" << (typeid(tsm_int).name()) <<endl;

    return 0;
}

结果:

vbnet 复制代码
D:\CWorkSpace\tsmTest\cmake-build-debug\tsmTest.exe
tsm 实际的类型是i

Process finished with exit code 0

可以看到 int 的类型是i, 从网上查了一下,由于编译器的差异导致的,并不是错误

我们来增加点难度

arduino 复制代码
#include <iostream>
#include <string>
#include <typeinfo>

using namespace std;

template<class  T>
auto get_decltype(T t) -> decltype(t) {
    return t;
}



int compare(int a,int b){
    if(a>b)
        return 1;
    else return  0;
}



int main(){
    const int i=45;

    // 有点类似 java 中的 二目三项式 ,只不过他指定的是左值, java 的 二目三项式指定的是右值
    conditional< (i>100),int,conditional< (i>50) ,double ,float >::type  >::type a;

    cout<< "tsm 实际的类型是" << (typeid(a).name()) <<endl;
    return 0;
}

结果:

vbnet 复制代码
D:\CWorkSpace\tsmTest\cmake-build-debug\tsmTest.exe
tsm 实际的类型是f

Process finished with exit code 0

这里面使用了 conditional 来指定 i 在不同范围 , a 的类型是不同的,

那么 conditional 的用法又是什么样子的呢

c 复制代码
////    conditional 简单用法
conditional<false,int,double>::type b;
cout<< "tsm 实际的类型是" << (typeid(b).name()) <<endl;

源码

arduino 复制代码
template<typename _Iftrue, typename _Iffalse>
  struct conditional<false, _Iftrue, _Iffalse>
  { typedef _Iffalse type; };

从源码看起来好像也比较简单, 如果是false 使用 _Iffalse 的typename ,如果是true使用 _Iftrue 的typename

conditional typeid decltype 都是在 template 中经常被用到的地方

相关推荐
比昨天多敲两行2 分钟前
C++入门基础
开发语言·c++
集3043 分钟前
C++多线程学习笔记
c++·笔记·学习
ComputerInBook41 分钟前
C++编程语言:标准库:第39章——本地化(语言环境)( Locales)(Bjarne Stroustrup)
c++·c++语言环境·c++ 本地化设置·c++ locale·c++ facet·语言特征
繁华似锦respect2 小时前
C++ 智能指针底层实现深度解析
linux·开发语言·c++·设计模式·代理模式
Bona Sun3 小时前
单片机手搓掌上游戏机(二十三)—esp32运行简单街机模拟器软硬件准备
c语言·c++·单片机
@小码农4 小时前
2025年北京海淀区中小学生信息学竞赛第二赛段C++真题
开发语言·数据结构·c++·算法
sulikey4 小时前
C++模板初阶详解:从函数模板到类模板的全面解析
开发语言·c++·模板·函数模板·类模板
0 0 04 小时前
CCF-CSP第39次认证第三题——HTTP 头信息(HPACK)【C++】
开发语言·c++·算法
汉克老师4 小时前
2023年海淀区中小学信息学竞赛复赛(小学组试题第二题 回文时间 (time))
c++·算法·北京海淀中小学信息竞赛·模拟法
还没想好取啥名5 小时前
C++11新特性(一)——原始字面量
开发语言·c++