为什么char不带符号或不带符号,而wchar_t是带符号的? [英] Why is char neither signed or unsigned, but wchar_t is?

查看:236
本文介绍了为什么char不带符号或不带符号,而wchar_t是带符号的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下C ++程序可正确编译:

The following C++ program compiles without errors:

void f(char){}
void f(signed char){}
void f(unsigned char){}
int main(){}  

同一程序的wchar_t版本没有:

void f(wchar_t){}
void f(signed wchar_t){}
void f(unsigned wchar_t){}
int main(){}

错误:重新定义了"void f(wchar_t)"
无效f(signed wchar_t){}

error: redefinition of ‘void f(wchar_t)’
void f(signed wchar_t){}

似乎wchar_tunsigned.
为什么超载不一致?

It seems that wchar_t is unsigned.
Why is there an inconsistency in overloading?

推荐答案

char都是不同的类型,可以重载

The chars are all distinct types and can be overloaded

[基本原理]/1

[...]普通charsigned charunsigned char是三种不同的类型, 统称为窄字符类型. [...]

[...] Plain char, signed char, and unsigned char are three distinct types, collectively called narrow character types. [...]

wchar_t也是一种独特的类型,但是不能用signedunsigned限定,它们只能与标准整数类型一起使用.

wchar_t is also a distinct type, but it cannot be qualified with signed or unsigned, which can only be used with the standard integer types.

[dcl.type]/2

[dcl.type] / 2

通常,最多允许一个 type-specifier 声明的完整 decl-specifier-seq type-specifier-seq trailing-type-specifier-seq .唯一的例外 该规则如下:

As a general rule, at most one type-specifier is allowed in the complete decl-specifier-seq of a declaration or in a type-specifier-seq or trailing-type-specifier-seq. The only exceptions to this rule are the following:

[...]

signedunsigned可以与charlongshortint组合.

signed or unsigned can be combined with char, long, short, or int.

[dcl.type.simple]/2

[dcl.type.simple] / 2

[...]表9总结了简单类型说明符及其指定类型的有效组合.

[...] Table 9 summarizes the valid combinations of simple-type-specifiers and the types they specify.

wchar_t的签名是由实现定义的:

The signedness of wchar_t is implementation defined:

[基本知识]/5

[...]类型wchar_t必须具有相同的大小,符号和对齐方式 要求(3.11)作为其他整数类型之一,称为 基础类型.

[...] Type wchar_t shall have the same size, signedness, and alignment requirements (3.11) as one of the other integral types, called its underlying type.

这篇关于为什么char不带符号或不带符号,而wchar_t是带符号的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆