c++ - typeid(equalizing) 中类型定义及其含义的区别

标签 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 =

unsigned short int = unsigned short

符号字符 = 字符

我想知道我上面提到的类型在 C 或 C++ 中是否相同? 如果是,在相互相等的同时想知道它们的含义,它们会再次相同吗? (EX: typeid(signed long int) == typeid(long))

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

最佳答案

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

  • 对于 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 .

关于c++ - typeid(equalizing) 中类型定义及其含义的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24784255/

相关文章:

c++ - 替换函数中的运行时错误?

c++ - 找不到 memset 标识符

c - 为什么我们在 c 中声明后使用 clrscr() 函数?

java - 在 Java 中确定 'type'

typescript - 如何从 typescript 中的类型中排除 getter only 属性

java - sql BIT 转 Java

c++ - 用结构修改 protected 值

c++ - 线性采样高斯模糊质量问题

在 C 中比较用户输入的字符

c - 哪些教程有助于理解硬件与 C 的交互