C++のいくつかのタイプについて
3901 ワード
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;
}