char!=(signed char),char!=(unsigned char)

use*_*956 40 c++ char

下面的代码编译,但char类型的行为与int类型的行为不同.

特别是

   cout << getIsTrue< isX<int8>::ikIsX  >() << endl;
   cout << getIsTrue< isX<uint8>::ikIsX  >() << endl;
   cout << getIsTrue< isX<char>::ikIsX  >() << endl;
Run Code Online (Sandbox Code Playgroud)

导致三种类型的模板的3个实例化:int8,uint8和char.是什么赋予了?

对于ints来说也是如此:int和uint32导致相同的模板实例化,而signed int则是另一个.

原因似乎是C++将char,signed char和unsigned char视为三种不同的类型.而int与signed int相同.这是对的还是我错过了什么?

#include <iostream>

using namespace std;

typedef   signed char       int8;
typedef unsigned char      uint8;
typedef   signed short      int16;
typedef unsigned short     uint16;
typedef   signed int        int32;
typedef unsigned int       uint32;
typedef   signed long long  int64;
typedef unsigned long long uint64;

struct TrueType {};
struct FalseType {};

template <typename T>
struct isX
{
   typedef typename T::ikIsX ikIsX;
};


// This  int==int32 is ambiguous
//template <>            struct isX<int  >    { typedef FalseType ikIsX; };  // Fails
template <>            struct isX<int32  >  { typedef FalseType ikIsX; };
template <>            struct isX<uint32 >  { typedef FalseType ikIsX; };


// Whay isn't this ambiguous? char==int8
template <>            struct isX<char  >  { typedef FalseType ikIsX; };
template <>            struct isX<int8  >  { typedef FalseType ikIsX; };
template <>            struct isX<uint8 >  { typedef FalseType ikIsX; };


template <typename T> bool getIsTrue();
template <>           bool getIsTrue<TrueType>() { return true; }
template <>           bool getIsTrue<FalseType>() { return false; }

int main(int, char **t )
{
   cout << sizeof(int8) << endl;  // 1
   cout << sizeof(uint8) << endl; // 1
   cout << sizeof(char) << endl;  // 1

   cout << getIsTrue< isX<int8>::ikIsX  >() << endl;
   cout << getIsTrue< isX<uint8>::ikIsX  >() << endl;
   cout << getIsTrue< isX<char>::ikIsX  >() << endl;

   cout << getIsTrue< isX<int32>::ikIsX  >() << endl;
   cout << getIsTrue< isX<uint32>::ikIsX  >() << endl;
   cout << getIsTrue< isX<int>::ikIsX  >() << endl;

}
Run Code Online (Sandbox Code Playgroud)

我正在使用g ++ 4.something

Gre*_*ers 62

以下是标准的答案:

3.9.1基本类型[basic.fundamental]

声明为characters(char)的对象应足够大,以存储实现的基本字符集的任何成员.如果此组中的字符存储在字符对象中,则该字符对象的整数值等于该字符的单个字符文字形式的值.实现定义char对象是否可以保存负值.可以显式声明字符unsignedsigned.平原char,signed charunsigned char三种不同的类型. A char,a signed char和a unsigned char 占用相同数量的存储空间并具有相同的对齐要求(basic.types); 也就是说,它们具有相同的对象表示.对于字符类型,对象表示的所有位都参与值表示.对于无符号字符类型,值表示的所有可能位模式表示数字.这些要求不适用于其他类型.在任何特定实现中,普通char对象可以采用与a signed char或a相同的值unsigned char; 哪一个是实现定义的.


Joh*_*itb 25

对于这样的问题,我想查看C的基本原理文档,它通常也提供了C++奥秘的答案,在阅读标准时有时会出现.它有这样的说法:

指定了三种类型的char:signed,plain和unsigned.普通字符可以表示为有符号或无符号,这取决于实现,如在先前实践中那样.引入了类型signed char,以便在将纯字符号作为unsigned实现的系统上提供一个单字节有符号整数类型.出于对称的原因,允许将关键字signed作为其他整数类型的类型名称的一部分.

C的基本原理

  • @Alcott我认为`char`可能是有符号的,也可能是无符号的,这是实现定义的,但是`signed char`总是有符号的,而`unsigned char`总是无符号的,如果你想确定/明确类型 (3认同)

Dre*_*ann 23

虽然大多数整数类型喜欢short并且int默认为存在signed,char但在C++中没有默认标志.

这是C++程序员在使用char8位整数类型时遇到的常见错误.

  • 用于比较short和int的+1. (2认同)
  • +1 因为您非常简洁地解释了数据类型的差异,并暗示应该如何通过比较来使用它们。 (2认同)
  • 历史脚注:我听说这是因为早期版本的 C 没有指定 char 的符号,因此不同的编译器做了不同的事情,然后标准保留了这种行为,以便旧代码可以继续在相同的编译器上工作。 (2认同)

Eva*_*ran 17

这是正确的,char,unsigned charsigned char是不同的类型.如果char它只是一个同义词signed charunsigned char取决于你的编译器实现,它可能会很好,但标准说它们是不同的类型.