本文介绍了为什么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

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

[...]

signedunsigned可以与charlongshortint组合.

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

[dcl.type.simple]/2

[dcl.type.simple] / 2

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

The signedness of wchar_t is implementation defined:

[基本知识]/5

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

06-13 14:22