对于 int, short, long, long long 类型, 增加 signed, 类型不变。
对于 char 类型, 增加 signed, 类型变了。 char 既不是 signed char, 也不是 unsigned char。
虽然 char 的取值范围, 一定是:要么是 signed char 范围, 要么是 unsigned char 范围。
Unlike the other integer types, there are three distinct basic character types: char, signed char, and unsigned char. In particular, char is not the same type as signed char. Although there are three character types, there are only two representations: signed and unsigned. The (plain) char type uses one of these representations. Which of the other two character representations is equivalent to char depends on the compiler.
如下代码在 Apple Clang 15.0.0 下运行
#include <iostream>
#include <type_traits>
template<typename T>
void hello()
{
std::cout << "hello<T>" << std::endl;
}
template<>
void hello<int>()
{
std::cout << "hello<int>" << std::endl;
}
template<>
void hello<signed char>()
{
std::cout << "hello<signed char>" << std::endl;
}
template<>
void hello<unsigned char>()
{
std::cout << "hello<unsigned char>" << std::endl;
}
int main()
{
std::cout << std::boolalpha;
std::cout << "int is signed int? " << std::is_same<int, signed int>::value << std::endl;
std::cout << "short is signed short? " << std::is_same<short, signed short>::value << std::endl;
std::cout << "long is signed long? " << std::is_same<long, signed long>::value << std::endl;
std::cout << "long long is signed long long? " << std::is_same<long long, signed long long>::value << std::endl;
std::cout << "char is signed char? " << std::is_same<char, signed char>::value << std::endl;
std::cout << "char is unsigned char? " << std::is_same<char, unsigned char>::value << std::endl;
std::cout << "------------\n";
std::cout << "hello<int>() is "; hello<int>();
std::cout << "hello<signed char>() is "; hello<signed char>();
std::cout << "hello<unsigned char>() is "; hello<unsigned char>();
std::cout << "hello<char>() is "; hello<char>();
return 0;
}
// result:
// int is signed int? true
// short is signed short? true
// long is signed long? true
// long long is signed long long? true
// char is signed char? false
// char is unsigned char? false
// ------------
// hello<int>() is hello<int>
// hello<signed char>() is hello<signed char>
// hello<unsigned char>() is hello<unsigned char>
// hello<char>() is hello<T>