为char默认提升? [英] Is char default-promoted?

查看:142
本文介绍了为char默认提升?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个愚蠢的问题,但可能有人请提供C ++ 11和C11标准参考:

This may be a silly question, but could someone please provide a standard reference for C++11 and C11:

字符默认晋升为 INT

Is char default-promoted to int?

这里有一个小背景:C和C ++有概念的默认参数推广的(C ++ 11 5.2.2 / 7; C11:6.5.2.2/6)。这需要在下面的调用,参数提升:

Here's a little background: Both C and C++ have notions of default argument promotion (C++11: 5.2.2/7; C11: 6.5.2.2/6). This entails that in the following call, the arguments are promoted:

void f(int, ...);

float a = 1; short int b = 2; char c = 'x';

f(0, a, b, c);

有关函数调用, A 转换为双击 B 转换为 INT 。但是,发生了什么 C ?我一直是IM pression的字符也被提升到 INT 下,但我找不到相关说法的标准。

For the function call, a is converted to double and b is converted to int. But what happens to c? I have always been under the impression that char also gets promoted to int, but I cannot find the relevant statement in the standards.

推荐答案

在C ++ 2011(ISO / IEC 14882:2011)的相关部分似乎是:

C++

In C++ 2011 (ISO/IEC 14882:2011), the relevant parts seem to be:

¶6函数可以声明为接受更少的参数(通过声明默认参数(8.3.6))或以上
  参数(通过使用省略号,...,或者一个功能参数包(8.3.5))比的参数的数量
  在函数的定义(8.4)。 [注:这意味着,除非省略号(...)或函数
  参数包时,一个参数可为每一个参数。末端注意事项】

§5.2.2 Function Call [expr.call]

¶6 A function can be declared to accept fewer arguments (by declaring default arguments (8.3.6)) or more arguments (by using the ellipsis, ..., or a function parameter pack (8.3.5)) than the number of parameters in the function definition (8.4). [Note: this implies that, except where the ellipsis (...) or a function parameter pack is used, a parameter is available for each argument. —end note]

¶7当存在对于给定的参数,该参数以这样的方式,接收传递任何参数
  函数可以通过调用的va_arg(18.10)获得参数的值。 [注:本段内容不
  适用于传递给函数参数包参数。功能参数包将在扩大
  模板实例化(14.5.3),因此,每个这样的参数都有相应的参数时功能
  模板特实际上是调用。末端注意】左值到右值(4.1),阵列到指针(4.2)和
  功能到指针(4.3)标准转换是在参数前pression执行。一种说法认为
  有(可能是CV-合格)类型的std :: nullptr_t转换为类型为void *(4.10)。这些转换后,
  如果参数没有算术,枚举,指针,指针成员或类类型,
  是病态的程序。有一个平凡的传递类类型的潜在评估参数(第9条)
  拷贝构造函数,一个不平凡的举动构造器,或者一个不平凡的析构函数,没有相应的
  参数,有条件支持与实现定义语义。

¶7 When there is no parameter for a given argument, the argument is passed in such a way that the receiving function can obtain the value of the argument by invoking va_arg (18.10). [Note: This paragraph does not apply to arguments passed to a function parameter pack. Function parameter packs are expanded during template instantiation (14.5.3), thus each such argument has a corresponding parameter when a function template specialization is actually called. —end note] The lvalue-to-rvalue (4.1), array-to-pointer (4.2), and function-to-pointer (4.3) standard conversions are performed on the argument expression. An argument that has (possibly cv-qualified) type std::nullptr_t is converted to type void* (4.10). After these conversions, if the argument does not have arithmetic, enumeration, pointer, pointer to member, or class type, the program is ill-formed. Passing a potentially-evaluated argument of class type (Clause 9) having a nontrivial copy constructor, a non-trivial move contructor, or a non-trivial destructor, with no corresponding parameter, is conditionally-supported with implementation-defined semantics.

如果该参数有积分
  或枚举类型,是受整体促销活动(4.5),或者浮点类型,是受
  以浮点促销(4.6),参数的值转换为升级后的类型前
  呼叫。这些促销活动被称为默认参数促销活动。

If the argument has integral or enumeration type that is subject to the integral promotions (4.5), or a floating point type that is subject to the floating point promotion (4.6), the value of the argument is converted to the promoted type before the call. These promotions are referred to as the default argument promotions.

我分开的最后两句话给他们的重视。他们在标准的第7段的连续一部分。

I've separated the last two sentences to give them emphasis. They're a continuous part of paragraph 7 in the standard.

¶1比布尔 char16_t char32_t其他整数类型的prvalue wchar_t的的整数转换
  排名(4.13)小于 INT 的等级可以转换为类型的prvalue INT 如果 INT 都可以重新present
  源类型的值;否则,源prvalue可以转换为类型的prvalue 无符号
  INT

§4.5 Integral promotions [conv.prom]

¶1 A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.

¶2类型的prvalue char16_t char32_t wchar_t的(3.9.1)可以转换到第一的一个prvalue
  以下几种类型,可以重新present其基础类型的所有值: INT unsigned int类型长整型
   unsigned long int类型得到long long int 无符号长long int类型。如果没有一个类型的在该列表中可以
  再present其基本类型,类型的prvalue的所有值 char16_t char32_t wchar_t的
  转换成它的基本类型的prvalue。

¶2 A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted to a prvalue of the first of the following types that can represent all the values of its underlying type: int, unsigned int, long int, unsigned long int, long long int, or unsigned long long int. If none of the types in that list can represent all the values of its underlying type, a prvalue of type char16_t, char32_t, or wchar_t can be converted to a prvalue of its underlying type.

C有两种情况,其中参数默认的提升。一个是当在范围为一个功能(第一被另一个答案覆盖)没有原型,并且第二时存在与省略号的原型。 C ++不允许第一种情况在所有的,当然。这些引号是从由另一种答案选择的标准相同的部分,但这里的片段也会比较长。他们是由标准的独立分析发现,只有当交叉检查,我注意到,部分是相同的这是。

C has two contexts where arguments are default promoted. One is when there is no prototype in scope for a function (first covered by another answer), and the second when there is a prototype with ellipsis. C++ does not allow the first case at all, of course. These quotes are from the same sections of the standard as chosen by another answer, but the snippets here are somewhat longer. They were found by independent analysis of the standard, and it was only when cross-checking that I noticed that the sections are the same.

在C 2011(ISO / IEC 9899:2011)的相关部分似乎是:

In C 2011 (ISO/IEC 9899:2011), the relevant parts seem to be:

¶6如果表示调用的函数的恩pression有一个类型不包括一
  原型,整数促销活动是在每个参数执行,论点
  有float类型都提升到两倍。这些被称为默认参数
  促销。如果参数的数量不等于的参数,数
  行为是不确定的。如果该函数被定义与一类,其中包括一个原型,并
  无论是样机以省略号( ... )或之后参数类型结束
  促销不是同类型的参数兼容的行为是不确定的。
  如果功能与不包含一个原型一个类型,类型的定义
  升级后的参数是不能与那些参数后兼容
  推广,行为是不确定的,除了以下情况:

§6.5.2.2 Function calls

¶6 If the expression that denotes the called function has a type that does not include a prototype, the integer promotions are performed on each argument, and arguments that have type float are promoted to double. These are called the default argument promotions. If the number of arguments does not equal the number of parameters, the behavior is undefined. If the function is defined with a type that includes a prototype, and either the prototype ends with an ellipsis (, ...) or the types of the arguments after promotion are not compatible with the types of the parameters, the behavior is undefined. If the function is defined with a type that does not include a prototype, and the types of the arguments after promotion are not compatible with those of the parameters after promotion, the behavior is undefined, except for the following cases:

- 一个提升的类型是有符号整数类型,其他类型的推广是
  对应的无符号整数类型,并且该值是在两种类型的重新presentable;

— one promoted type is a signed integer type, the other promoted type is the corresponding unsigned integer type, and the value is representable in both types;

- 这两种类型都指向资格或不合格的版本字符类型或
  无效的。

— both types are pointers to qualified or unqualified versions of a character type or void.

¶7如果表示调用的函数的前pression有一个类型,做包括样机,
  参数被隐式转换,仿佛被分配到类型
  对应的参数,同时考虑各参数的类型是不合格的版本
  其宣称的类型。在函数原型声明符原因省略号
  参数类型转换到最后声明参数后停止。默认参数
  促销活动是在尾随参数执行。

¶7 If the expression that denotes the called function has a type that does include a prototype, the arguments are implicitly converted, as if by assignment, to the types of the corresponding parameters, taking the type of each parameter to be the unqualified version of its declared type. The ellipsis notation in a function prototype declarator causes argument type conversion to stop after the last declared parameter. The default argument promotions are performed on trailing arguments.

在'整数促销在§6.3.1.1定义的:

The 'integer promotions' are defined in §6.3.1.1:

¶1每个整数类型定义了一个整数转换排名如下:

§6.3.1 Arithmetic operands

§6.3.1.1 Boolean, characters, and integers

¶1 Every integer type has an integer conversion rank defined as follows:

- 没有两个符号整数类型应具有相同的等级,即使他们倍感相同
  再presentation。

— No two signed integer types shall have the same rank, even if they hav e the same representation.

- 有符号整数类型的秩应比任何符号整数军衔更大
  用更少的precision类型。

— The rank of a signed integer type shall be greater than the rank of any signed integer type with less precision.

- 得到long long int的等级应高于长整型军衔,其中较大
  应大于INT的等级更大,这应大于短军衔更大
  int,它应大于符号字符的排名更高。

— The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.

- 任何无符号整数类型的秩应等于相应的军衔
  符号整型,如果有的话。

— The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type, if any.

- 任何标准整数类型的级别应该比任何扩展的排名更高
  整数型具有相同的宽度。

— The rank of any standard integer type shall be greater than the rank of any extended integer type with the same width.

- 字符的秩应等于签署char和unsigned char型的秩

— The rank of char shall equal the rank of signed char and unsigned char.

- _Bool的秩应小于其他所有标准的整数类型等级

— The rank of _Bool shall be less than the rank of all other standard integer types.

- 任何枚举类型的秩应等于兼容的整数类型的秩
  (见6.7.2.2)。

— The rank of any enumerated type shall equal the rank of the compatible integer type (see 6.7.2.2).

- 任何扩展符号整型相对于另一个扩展签署的秩
  与同precision整数类型是实现定义的,但仍受制于
  其他规则确定整数转换等级。

— The rank of any extended signed integer type relative to another extended signed integer type with the same precision is implementation-defined, but still subject to the other rules for determining the integer conversion rank.

- 对于所有的整数类型T1,T2和T3,如果T1比T2和T2具有更大秩
  比T3更大的等级,然后T1具有较大的比排名T3。

— For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 has greater rank than T3.

¶2以下可在离pression用于任何一个 INT unsigned int类型
  使用

¶2 The following may be used in an expression wherever an int or unsigned int may be used:

- 一个对象或前pression一个整型(比 INT 其他或 unsigned int类型
  其整数转换等级小于或等于 INT 的等级和
   unsigned int类型

— An object or expression with an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int.

- 类型的位字段 _Bool INT 符号int unsigned int类型

— A bit-field of type _Bool, int, signed int, or unsigned int.

如果一个 INT 可以重新present原始类型的所有值(由宽度的限制,对于
  位字段),值转换为 INT ;否则,将其转换为符号
  INT
。这些被称为的整数促销 58)所有其他类型是不变的
  整型的提升。

If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions.58) All other types are unchanged by the integer promotions.

58)整数促销活动仅适用于:作为通常的算术转换的一部分,一定
  参数前pressions,以一元+的操作数, - 和〜运营商,并在2002两个操作数
  运营商转变,由各自的小节中规定。

58) The integer promotions are applied only: as part of the usual arithmetic conversions, to certain argument expressions, to the operands of the unary +, -, and ~ operators, and to both operands of the shift operators, as specified by their respective subclauses.

我注意到,在同一时间,问题所列的功能无效F(...); ,这是一个C ++函数,而不是一个C函数; C没有让省略号出现作为唯一的参数传递给函数。这个问题已经被更新,以无效F(INT,...); 这是C和C ++有效


I note that at one time, the question listed the function void f(...);, which is a C++ function and not a C function; C does not allow the ellipsis to appear as the only argument to the function. The question has since been updated to void f(int, ...); which is valid in both C and C++.

这篇关于为char默认提升?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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