类型定义之间的差异及其在typeid(均衡)中的含义

时间:2014-07-16 15:05:44

标签: c++ c types typeid

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)

答案:http://cpp.sh/3rm

1 个答案:

答案 0 :(得分:2)

有些类型相同,有些类型不同(=不相同):

  • 代表char,添加signedunsigned会为您提供三种不同的类型。
  • 代表shortintlonglong longsigned是隐含的,并且添加不执行任何操作。添加unsigned会为您提供一组新的不同类型。
  • unsignedshortlonglong long后面可以跟int,但如果int存在与否{}不要给出不同的类型。

原则上,所有不同类型的typeid应该是不同的。这意味着模板和函数重载决策将这些视为不同的类型,而例如传递shortshort 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

Live demo here