unsigned int
= unsigned
int
= signed int
= signed
signed long long int
= singed long long
= long long
unsigned long long int
= unsigned long long
signed long int
= signed long
= long
unsigned long int
= unsigned long
signed short int
= signed short
= short
unsigned short int
= unsigned short
signed char
= char
我想知道我在C或C ++中提到的类型是否相同?
如果它是,在相互平等的同时想知道它们的含义,它们会再次相同吗? (EX:typeid(signed long int)
== typeid(long)
)
答案 0 :(得分:2)
有些类型相同,有些类型不同(=不相同):
char
,添加signed
和unsigned
会为您提供三种不同的类型。short
,int
,long
和long long
,signed
是隐含的,并且添加不执行任何操作。添加unsigned
会为您提供一组新的不同类型。unsigned
,short
,long
和long long
后面可以跟int
,但如果int
存在与否{}不要给出不同的类型。原则上,所有不同类型的typeid
应该是不同的。这意味着模板和函数重载决策将这些视为不同的类型,而例如传递short
和short int
都会调用相同的重载。
据我所知,这些规则对于C和C ++是相同的。如果我犯了一个错误,请告诉我,我正在写下这个问题。
要检查这一点,您可以使用static_assert
结合std::is_same
进行编译时检查:
#include <type_traits>
using std::is_same;
static_assert(!is_same<char, signed char>(), "");
static_assert(!is_same<char, unsigned char>(), "");
static_assert(!is_same<unsigned char, signed char>(), "");
static_assert( is_same<short, signed short>(), "");
static_assert( is_same<int, signed int>(), "");
static_assert( is_same<long, signed long>(), "");
static_assert( is_same<long long, signed long long>(), "");
static_assert(!is_same<short, unsigned short>(), "");
static_assert(!is_same<int, unsigned int>(), "");
static_assert(!is_same<long, unsigned long>(), "");
static_assert(!is_same<long long, unsigned long long>(), "");
static_assert( is_same<unsigned int, unsigned>(), "");
static_assert( is_same<short int, short>(), "");
static_assert( is_same<long int, long>(), "");
static_assert( is_same<long long int, long long>(), "");
int main(){} // only included to make the program link