当前位置 : 主页 > 编程语言 > c语言 >

获取C++变量类型的简单方法

来源:互联网 收集:自由互联 发布时间:2023-02-01
目录 获取C++变量类型 与传统方法的对比 获取C++数据类型取值范围 包含头文件 类型变量定义 取类型值范围 完整代码 获取C++变量类型 直接上代码 #include type_traits#include typeinfo#include m
目录
  • 获取C++变量类型
    • 与传统方法的对比
  • 获取C++数据类型取值范围
    • 包含头文件
    • 类型变量定义
    • 取类型值范围
    • 完整代码

获取C++变量类型

直接上代码

#include <type_traits>
#include <typeinfo>
#include <memory>
#include <string>
#include <cstdlib>
#include <iostream>
#ifndef _MSC_VER
#include <cxxabi.h>
#endif

using namespace std;

template <class T>
string type_name()
{
    typedef typename remove_reference<T>::type TR;
    unique_ptr<char, void (*)(void *)> own(
#ifndef _MSC_VER
        abi::__cxa_demangle(typeid(TR).name(), nullptr, nullptr, nullptr),
#else
        nullptr,
#endif
        free);
    string r = own != nullptr ? own.get() : typeid(TR).name();
    if (is_const<TR>::value)
        r += " const";
    if (is_volatile<TR>::value)
        r += " volatile";
    if (is_lvalue_reference<T>::value)
        r += "&";
    else if (is_rvalue_reference<T>::value)
        r += "&&";
    return r;
}

int main()
{
    int a = 1;
    const int &b = a;
    cout << type_name<decltype(b)>() << endl;
}

定义了一个模板函数type_name(),可以对传入的模板类型T进行类型判断,结合指针、左值/右值引用、常类型,准确得出变量的类型。在调用该函数时使用了decltype关键字,传入待确定的变量,然后输出变量类型。

以上运行的结果为

int const&

如果要输出int指针的类型:

int main()
{
    int a = 1;
    int* b = &a;
    cout << type_name<decltype(b)>() << endl;
}

结果为:

int*

可以看出该函数得出的结果是非常准确而又精简的。

与传统方法的对比

传统输出C++类型的方法是使用typeid(var).name(),其弊端在于输出的结果不完整或者过于冗长。

例如:

1. 传统方法打印int类型

#include <iostream>

int main()
{
  int a = 10;
  std::cout << typeid(a).name() << std::endl;
}

运行结果为:

i

只得到了int的缩写i。

2. 传统方法打印string类型

#include <iostream>
#include <string>

int main()
{
  std::string a = "abc";
  std::cout << typeid(a).name() << std::endl;
}

运行结果为:

NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

过于冗长,不便于看出类型名称。

3. 传统方法打印引用类型

#include <iostream>

int main()
{
  int a0 = 23;
  int& a = a0;
  std::cout << typeid(a).name() << std::endl;
}

运行结果为:

i

并没有准确地输出我们想要的结果。

总结:使用了稍微复杂的模板函数,可以准确输出C++的变量类型,而非大多数人提到的type_id(var).name()打印出来的结果有各种不恰当的地方。

获取C++数据类型取值范围

包含头文件

#include <limits>
#include <float.h>

类型变量定义

int a = 2;//32位整数 4字节 4byte  32bit
    unsigned int b = 2u;//无符号32位整数  4字节 4byte  32bit
    signed int b1 = -2147483648;//有符号32位整数  4字节 4byte  32bit
    long c = 2l;//32位整数 4字节 4byte  32bit
    unsigned long d= 2ul;//无符号32位整数 4字节 4byte  32bit
    double e = 2.0;//双精度浮点数
    float f = 2.0f;//单精度浮点数
    long double g=2.0l;//长双精度浮点数
    long long h=2ll;//超长整数 64位整数 8字节 8byte  64bit
    short i = 2;//16位整数 2字节 2byte  16bit
    unsigned short i1 = 2;//无符号16位整数 2字节 2byte  16bit
    char j = '2';//字符类型 1字节 1byte  8bit
    char j1 = '2';//无符号字符类型 1字节 1byte  8bit

取类型值范围

std::cout <<"int a is :"<< a << "  int类型取值范围:" <<INT_MIN <<","<<INT_MAX <<"\n"
              <<"signed int b1 is :"<< b1 << "  int类型取值范围: " <<INT_MIN <<","<<INT_MAX <<"\n"
              <<"unsigned int b is : "<<b << "  unsigned int类型取值范围: " <<0 <<","<<UINT_MAX <<"\n"
              <<"long c is : "<<c<<"  long类型取值范围: " <<LONG_MIN <<","<<LONG_MAX <<"\n"
              <<"unsigned long d is : "<<d<<"  unsigned long类型取值范围: " <<0 <<","<<ULONG_MAX <<"\n"
              <<"double e is : "<<e<< "  double类型取值范围:" <<DBL_MIN <<","<<DBL_MAX <<"\n"
              <<"float f is : "<<f<<"  float类型取值范围:" <<FLT_MIN <<","<<FLT_MAX <<"\n"
              <<"long double g is : "<<g<<"  long double类型取值范围:" <<LDBL_MIN <<","<<LDBL_MAX <<"\n"
              <<"long long h is : "<<h<<"  long long类型取值范围: " <<LLONG_MIN <<","<<LLONG_MAX <<"\n"
              <<"short i is : "<<i<<"  short类型取值范围: " <<SHRT_MIN <<","<<SHRT_MAX <<"\n"
              <<"unsigned short i is : "<<i1<<"  unsigned short类型取值范围: " <<0 <<","<<USHRT_MAX <<"\n"
              <<"char j is : "<<j<<"  char类型取值范围: " <<CHAR_MIN <<","<<CHAR_MAX <<"\n"
              <<"unsigned char j1 is : "<<j1<<" unsigned char类型取值范围: " <<0 <<","<<UCHAR_MAX <<"\n";

输出效果:

int a is :2  int类型取值范围:-2147483648,2147483647
signed int b1 is :-2147483648  int类型取值范围: -2147483648,2147483647
unsigned int b is : 2  unsigned int类型取值范围: 0,4294967295
long c is : 2  long类型取值范围: -9223372036854775808,9223372036854775807
unsigned long d is : 2  unsigned long类型取值范围: 0,18446744073709551615
double e is : 2  double类型取值范围:2.22507e-308,1.79769e+308
float f is : 2  float类型取值范围:1.17549e-38,3.40282e+38
long double g is : 2  long double类型取值范围:3.3621e-4932,1.18973e+4932
long long h is : 2  long long类型取值范围: -9223372036854775808,9223372036854775807
short i is : 2  short类型取值范围: -32768,32767
unsigned short i is : 2  unsigned short类型取值范围: 0,65535
char j is : 2  char类型取值范围: -128,127
unsigned char j1 is : 2 unsigned char类型取值范围: 0,255

完整代码

#include <iostream>
#include <chrono>
#include <thread>
#include <limits>
#include <float.h>
 
int main() {
    int a = 2;//32位整数 4字节 4byte  32bit
    unsigned int b = 2u;//无符号32位整数  4字节 4byte  32bit
    signed int b1 = -2147483648;//有符号32位整数  4字节 4byte  32bit
    long c = 2l;//32位整数 4字节 4byte  32bit
    unsigned long d= 2ul;//无符号32位整数 4字节 4byte  32bit
    double e = 2.0;//双精度浮点数
    float f = 2.0f;//单精度浮点数
    long double g=2.0l;//长双精度浮点数
    long long h=2ll;//超长整数 64位整数 8字节 8byte  64bit
    short i = 2;//16位整数 2字节 2byte  16bit
    unsigned short i1 = 2;//无符号16位整数 2字节 2byte  16bit
    char j = '2';//字符类型 1字节 1byte  8bit
    char j1 = '2';//无符号字符类型 1字节 1byte  8bit
    //进度表示写法
    int bin2 =0b11111111;//二进制 0和1 前缀:0b  stdc++ 14
    int bin8 = 077;//八进制  0~7  前缀:0
    int bin16= 0xff;//十六进制 0~F 前缀:0x
    int aa = INT_MAX * 2 + 1;
    unsigned  int bb = UINT_MAX;
    if (aa == bb)
    {
        std::cout<<UINT_MAX<<std::endl;
    }
    std::cout <<"int a is :"<< a << "  int类型取值范围:" <<INT_MIN <<","<<INT_MAX <<"\n"
              <<"signed int b1 is :"<< b1 << "  int类型取值范围: " <<INT_MIN <<","<<INT_MAX <<"\n"
              <<"unsigned int b is : "<<b << "  unsigned int类型取值范围: " <<0 <<","<<UINT_MAX <<"\n"
              <<"long c is : "<<c<<"  long类型取值范围: " <<LONG_MIN <<","<<LONG_MAX <<"\n"
              <<"unsigned long d is : "<<d<<"  unsigned long类型取值范围: " <<0 <<","<<ULONG_MAX <<"\n"
              <<"double e is : "<<e<< "  double类型取值范围:" <<DBL_MIN <<","<<DBL_MAX <<"\n"
              <<"float f is : "<<f<<"  float类型取值范围:" <<FLT_MIN <<","<<FLT_MAX <<"\n"
              <<"long double g is : "<<g<<"  long double类型取值范围:" <<LDBL_MIN <<","<<LDBL_MAX <<"\n"
              <<"long long h is : "<<h<<"  long long类型取值范围: " <<LLONG_MIN <<","<<LLONG_MAX <<"\n"
              <<"short i is : "<<i<<"  short类型取值范围: " <<SHRT_MIN <<","<<SHRT_MAX <<"\n"
              <<"unsigned short i is : "<<i1<<"  unsigned short类型取值范围: " <<0 <<","<<USHRT_MAX <<"\n"
              <<"char j is : "<<j<<"  char类型取值范围: " <<CHAR_MIN <<","<<CHAR_MAX <<"\n"
              <<"unsigned char j1 is : "<<j1<<" unsigned char类型取值范围: " <<0 <<","<<UCHAR_MAX <<"\n";
 
    std::cout<<"二进制0b11111111值是:"<<bin2<<std::endl;
    std::cout<<"八进制077值是:"<<bin8<<std::endl;
    std::cout<<"十六进制0xff值是:"<<bin16<<std::endl;
 
 
    std::cout << "等待5秒后退出程序" <<std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(5));
 
    return 0;
}

以上为个人经验,希望能给大家一个参考,也希望大家多多支持自由互联。

上一篇:详解C/C++实现各种字符转换方法合集
下一篇:没有了
网友评论