int8_t和uint8_t是否打算为char类型? [英] Are int8_t and uint8_t intended to be char types?
问题描述
鉴于这个C ++ 11程序,我应该看到一个数字还是一个字母?还是没有期望?
Given this C++11 program, should I expect to see a number or a letter? Or not make expectations?
#include <cstdint>
#include <iostream>
int main()
{
int8_t i = 65;
std::cout << i;
}
标准是否指定此类型可以还是字符类型?
Does the standard specify whether this type can or will be a character type?
推荐答案
从C ++ 0x FDIS(N3290)的第18.4.1节[cstdint.syn]开始,int8_t
是一个可选的typedef,其指定如下: :
From § 18.4.1 [cstdint.syn] of the C++0x FDIS (N3290), int8_t
is an optional typedef that is specified as follows:
namespace std {
typedef signed integer type int8_t; // optional
//...
} // namespace std
第3.9.1节[基本,基本]状态:
§ 3.9.1 [basic.fundamental] states:
有五种标准有符号整数类型:"
signed char
","short int
","int
","long int
"和"long long int
".在此列表中,每种类型提供的存储量至少与列表中位于其前面的类型相同.可能还有实现定义的扩展有符号整数类型.标准和扩展的有符号整数类型统称为有符号整数类型.
There are five standard signed integer types: "
signed char
", "short int
", "int
", "long int
", and "long long int
". In this list, each type provides at least as much storage as those preceding it in the list. There may also be implementation-defined extended signed integer types. The standard and extended signed integer types are collectively called signed integer types.
...
类型bool
,char
,char16_t
,char32_t
,wchar_t
和有符号和无符号整数类型统称为整数类型.整数类型的同义词是整数类型.
Types bool
, char
, char16_t
, char32_t
, wchar_t
, and the signed and unsigned integer types are collectively called integral types. A synonym for integral type is integer type.
第3.9.1节还规定:
§ 3.9.1 also states:
在任何特定的实现中,普通的
char
对象可以采用与signed char
或unsigned char
相同的值;哪个是实现定义的.
In any particular implementation, a plain
char
object can take on either the same values as asigned char
or anunsigned char
; which one is implementation-defined.
很容易得出结论,只要char
对象采用带符号值,则int8_t
可能是char
的typedef.但是,情况并非如此,因为char
不在有符号整数类型(标准和可能扩展的有符号整数类型)的列表中.另请参见 Stephan T. Lavavej的评论在std::make_unsigned
和std::make_signed
上.
It is tempting to conclude that int8_t
may be a typedef of char
provided char
objects take on signed values; however, this is not the case as char
is not among the list of signed integer types (standard and possibly extended signed integer types). See also Stephan T. Lavavej's comments on std::make_unsigned
and std::make_signed
.
因此,int8_t
是signed char
的typedef,或者是扩展的带符号整数类型,其对象恰好占据了8位存储空间.
Therefore, either int8_t
is a typedef of signed char
or it is an extended signed integer type whose objects occupy exactly 8 bits of storage.
但是,要回答您的问题,您不应做任何假设.因为已经定义了x.operator<<(y)
和operator<<(x,y)
两种形式的函数,所以第13.5.3节[over.binary]表示我们参考第13.3.1.2节[over.match.oper]来确定std::cout << i
的解释. §13.3.1.2依次说,实现应根据§13.3.2和§13.3.3从候选函数集中进行选择.然后,我们根据§13.3.3.2 [over.ics.rank]确定:
To answer your question, though, you should not make assumptions. Because functions of both forms x.operator<<(y)
and operator<<(x,y)
have been defined, § 13.5.3 [over.binary] says that we refer to § 13.3.1.2 [over.match.oper] to determine the interpretation of std::cout << i
. § 13.3.1.2 in turn says that the implementation selects from the set of candidate functions according to § 13.3.2 and § 13.3.3. We then look to § 13.3.3.2 [over.ics.rank] to determine that:
- 如果
int8_t
是signed char
的精确匹配项(即signed char
的typedef),则会调用template<class traits> basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char)
模板. - 否则,将
int8_t
升级为int
,并调用basic_ostream<charT,traits>& operator<<(int n)
成员函数.
- The
template<class traits> basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char)
template would be called ifint8_t
is an Exact Match forsigned char
(i.e. a typedef ofsigned char
). - Otherwise, the
int8_t
would be promoted toint
and thebasic_ostream<charT,traits>& operator<<(int n)
member function would be called.
对于u
是uint8_t
对象的std::cout << u
:
- 如果
uint8_t
是unsigned char
的完全匹配项,则将调用template<class traits> basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char)
模板. - 否则,由于
int
可以表示所有uint8_t
值,因此uint8_t
将被提升为int
,并且将调用basic_ostream<charT,traits>& operator<<(int n)
成员函数.
- The
template<class traits> basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char)
template would be called ifuint8_t
is an Exact Match forunsigned char
. - Otherwise, since
int
can represent alluint8_t
values, theuint8_t
would be promoted toint
and thebasic_ostream<charT,traits>& operator<<(int n)
member function would be called.
如果您始终要打印字符,最安全,最清晰的选择是:
If you always want to print a character, the safest and most clear option is:
std::cout << static_cast<signed char>(i);
如果您总是想打印一个数字:
And if you always want to print a number:
std::cout << static_cast<int>(i);
这篇关于int8_t和uint8_t是否打算为char类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!