Home  >  Article  >  Backend Development  >  The following are English question and answer titles generated based on the article content you provided: Why does `char` behave differently from integer types in template instantiation when comparing `char`, `signed char`, and `unsigned char`?

The following are English question and answer titles generated based on the article content you provided: Why does `char` behave differently from integer types in template instantiation when comparing `char`, `signed char`, and `unsigned char`?

Linda Hamilton
Linda HamiltonOriginal
2024-10-25 22:00:28925browse

以下是根据您提供的文章内容生成的英文问答类标题:

Why does `char` behave differently from integer types in template instantiation when comparing `char`, `signed char`, and `unsigned char`?

differences in behavior between char, signed char and unsigned char

The code below compiles successfully, but char behaves differently than integer types.

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

The result is three instantiation patterns for three types: int8, uint8 and char. Why does this happen?

The same is not true for integers: int and uint32 result in one pattern instantiation, and signed int results in another.

The reason is probably because C treats char , signed char and unsigned char as three different types. While int is the same as signed int. Is this true or am I missing something?

<code class="cpp">#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;
};


// Это  int==int32 неоднозначно
//template <>            struct isX<int  >    { typedef FalseType ikIsX; };  // Ошибка
template <>            struct isX<int32  >  { typedef FalseType ikIsX; };
template <>            struct isX<uint32 >  { typedef FalseType ikIsX; };


// Почему это не двусмысленно? 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;

}

I'm using g 4.something

The above is the detailed content of The following are English question and answer titles generated based on the article content you provided: Why does `char` behave differently from integer types in template instantiation when comparing `char`, `signed char`, and `unsigned char`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn