相关疑难解决方法(0)

char!=(signed char),char!=(unsigned 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 …
Run Code Online (Sandbox Code Playgroud)

c++ char

40
推荐指数
4
解决办法
2万
查看次数

从unsigned char*到char*的转换无效

这是一个代码 -

  1 int main(int argc, char *argv[])
  2 {
  3     signed char S, *psc;
  4     unsigned char U,  *pusc;
  5     char C, *pc;
  6 
  7     C = S;
  8     C = U;
  9 
 10     pc = psc;
 11     pc = pusc;
 12 
 13     return 0;
 14 }

$ gcc test.cpp -o a
test.cpp: In function ‘int main(int, char**)’:
test.cpp:10:7: error: invalid conversion from ‘signed char*’ to ‘char*’ [-fpermissive]
test.cpp:11:7: error: invalid conversion from ‘unsigned char*’ to ‘char*’ [-fpermissive]
Run Code Online (Sandbox Code Playgroud)

这是在英特尔32位机器上的Ubuntu …

c++ signed char unsigned-char

12
推荐指数
2
解决办法
2万
查看次数

标签 统计

c++ ×2

char ×2

signed ×1

unsigned-char ×1