关于C++中的一些类型

void printTypeSize(void){
    std::cout << "bool size             :" << sizeof(bool) << std::endl;
    std::cout << "unsigned char size    :" << sizeof(unsigned char) << std::endl;
    std::cout << "char size             :" << sizeof( char) << std::endl;
    std::cout << "unsigned short size   :" << sizeof(unsigned short) << std::endl;
    std::cout << "short size            :" << sizeof(short ) << std::endl;
    std::cout << "unsigned int size     :" << sizeof(unsigned int) << std::endl;
    std::cout << "int size              :" << sizeof(int) << std::endl;

    std::cout << "float size            :" << sizeof(float) << std::endl;
    std::cout << "double size           :" << sizeof(double) << std::endl;
    std::cout << "long size             :" << sizeof(long) << std::endl;
    std::cout << "long double size      :" << sizeof(long double) << std::endl;
}

 

比如我的机器输出如下

bool size             :1
unsigned char size    :1
char size             :1
unsigned short size   :2
short size            :2
unsigned int size     :4
int size              :4
float size            :4
double size           :8
long size             :8
long double size      :16

 这是一个可以打印变量类型的函数,只包括了基本的算数类型,其他类型同理


void printTypeID(void){
    std::cout << "bool              :" << typeid(bool).name() << std::endl;
    std::cout << "unsigned char     :" << typeid(unsigned char).name() << std::endl;
    std::cout << "char              :" << typeid( char).name() << std::endl;
    std::cout << "unsigned short    :" << typeid(unsigned short).name() << std::endl;
    std::cout << "short             :" << typeid(short ).name() << std::endl;
    std::cout << "unsigned int      :" << typeid(unsigned int).name() << std::endl;
    std::cout << "int               :" << typeid(int).name() << std::endl;

    std::cout << "float             :" << typeid(float).name() << std::endl;
    std::cout << "double            :" << typeid(double).name() << std::endl;
    std::cout << "long              :" << typeid(long).name() << std::endl;
    std::cout << "long double       :" << typeid(long double).name() << std::endl;
}
template< typename T>
void printTypeName(T value){
    std::string type;
    if(typeid(value).name() == typeid(bool).name()) type = "bool";
    else if(typeid(value).name() == typeid(unsigned char).name()) type = "unsigned char";
    else if(typeid(value).name() == typeid(char).name()) type = "char";
    else if(typeid(value).name() == typeid(unsigned short).name()) type = "unsigned short";
    else if(typeid(value).name() == typeid(short).name()) type = "short";
    else if(typeid(value).name() == typeid(unsigned int).name()) type = "unsigned int";
    else if(typeid(value).name() == typeid(int).name()) type = "int";
    else if(typeid(value).name() == typeid(float).name()) type = "float";
    else if(typeid(value).name() == typeid(double).name()) type = "double";
    else if(typeid(value).name() == typeid(long).name()) type = "long";
    else if(typeid(value).name() == typeid(long double).name()) type = "long double";
    else type = "unkonw type";
    std::cout << type << std::endl;
}

 

你可能感兴趣的:(C++)