Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Usual arithmetic conversions

      From cppreference.com
      <cpp‎ |language
       
       
      C++ language
      General topics
      Flow control
      Conditional execution statements
      Iteration statements (loops)
      Jump statements
      Functions
      Function declaration
      Lambda function expression
      inline specifier
      Dynamic exception specifications(until C++17*)
      noexcept specifier(C++11)
      Exceptions
      Namespaces
      Types
      Specifiers
      constexpr(C++11)
      consteval(C++20)
      constinit(C++20)
      Storage duration specifiers
      Initialization
      Expressions
      Alternative representations
      Literals
      Boolean -Integer -Floating-point
      Character -String -nullptr(C++11)
      User-defined(C++11)
      Utilities
      Attributes(C++11)
      Types
      typedef declaration
      Type alias declaration(C++11)
      Casts
      Memory allocation
      Classes
      Class-specific function properties
      Special member functions
      Templates
      Miscellaneous
       
      Expressions
      General
      Literals
      Operators
      Conversions
       

      Many binary operators that expect operands ofarithmetic orenumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called theusual arithmetic conversions.

      Contents

      [edit]Definition

      Usual arithmetic conversions are defined as follows:

      [edit]Stage 1

      Applieslvalue-to-rvalue conversion to both operands, the resulting prvalues are used in place of the original operands for the remaining process.

      [edit]Stage 2

      • If either operand is ofscoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
      • Otherwise, proceed to the next stage.
      (since C++11)

      [edit]Stage 3

      • If either operand is ofenumeration type, and the other operand is of a different enumeration type or a floating-point type, the expression is ill-formed.
      • Otherwise, proceed to the next stage.
      (since C++26)

      [edit]Stage 4

      • If both operands have the same type, no further conversion will be performed.
      • Otherwise, if one of the operands is of a non-floating-point type, that operand is converted to the type of the other operand.
      • Otherwise, if thefloating-point conversion ranks of the types of the operands areordered but(since C++23) not equal, then the operand of the type with the lesser floating-point conversion rank is converted to the type of the other operand.
      • Otherwise, if the floating-point conversion ranks of the types of the operands are equal, then the operand with the lesserfloating-point conversion subrank is converted to the type of the other operand.
      • Otherwise, the expression is ill-formed.
      (since C++23)
      • Otherwise, both operands are of integer types, proceed to the next stage.

      [edit]Stage 5

      Both operands are converted to a common typeC. Given the typesT1 andT2 as the promoted type (under the rules of integral promotions) of the operands, the following rules are applied to determineC:

      • IfT1 andT2 are the same type,C is that type.
      • Otherwise, ifT1 andT2 are both signed integer types or both unsigned integer types,C is the type of greaterinteger conversion rank.
      • Otherwise, one type betweenT1 andT2 is an signed integer typeS, the other type is an unsigned integer typeU. Apply the following rules:
      • If the integer conversion rank ofU is greater than or equal to the integer conversion rank ofS,C isU.
      • Otherwise, ifS can represent all of the values ofU,C isS.
      • Otherwise,C is the unsigned integer type corresponding toS.

      If one operand is of enumeration type and the other operand is of a different enumeration type or a floating-point type, this behavior is deprecated.

      (since C++20)
      (until C++26)

      [edit]Integer conversion rank

      Everyinteger type has aninteger conversion rank defined as follows:

      • No two signed integer types other thanchar andsignedchar (ifchar is signed) have the same rank, even if they have the same representation.
      • The rank of a signed integer type is greater than the rank of any signed integer type with a smaller width.
      • The ranks of the following integer types decrease in order:
      • longlong
      (since C++11)
      • long
      • int
      • short
      • signedchar
      • The rank of any unsigned integer type equals the rank of the corresponding signed integer type.
      • The rank of any standard integer type is greater than the rank of any extended integer type with the same width.
      (since C++11)
      • The rank ofbool is less than the rank of all standard integer types.
      • The ranks of encoding character types (char,char8_t(since C++20),char16_t,char32_t,(since C++11) andwchar_t) equal the ranks of theirunderlying types, which means:
      • The rank ofchar equals the rank ofsignedchar andunsignedchar.
      • The rank ofchar8_t equals the rank ofunsignedchar.
      (since C++20)
      (since C++11)
      • The rank ofwchar_t equals the rank of its implementation-defined underlying type.
      • The rank of any extended signed integer type relative to another extended signed integer type with the same width is implementation-defined, but still subject to the other rules for determining the integer conversion rank.
      (since C++11)
      • For all integer typesT1,T2, andT3, ifT1 has greater rank thanT2 andT2 has greater rank thanT3, thenT1 has greater rank thanT3.

      The integer conversion rank is also used in the definition ofintegral promotion.

      [edit]Floating-point conversion rank and subrank

      [edit]Floating-point conversion rank

      Everyfloating-point type has afloating-point conversion rank defined as follows:

      • The ranks of the standard floating-point types decrease in order:
        • longdouble
        • double
        • float
      • The rank of a floating-point typeT is greater than the rank of any floating-point type whose set of values is a proper subset of the set of values ofT.
      • Two extended floating-point types with the same set of values have equal ranks.
      • An extended floating-point type with the same set of values as exactly one cv-unqualified standard floating-point type has a rank equal to the rank of that standard floating-point type.
      • An extended floating-point type with the same set of values as more than one cv-unqualified standard floating-point type has a rank equal to the rank ofdouble.
      (since C++23)


      Floating-point conversion subrank

      Floating-point types that have equal floating-point conversion ranks are ordered byfloating-point conversion subrank. The subrank forms a total order among types with equal ranks.

      The typesstd::float16_t,std::float32_t,std::float64_t, andstd::float128_t (fixed width floating-point types) have a greater conversion subrank than any standard floating-point type with equal conversion rank. Otherwise, the conversion subrank order is implementation-defined.

      (since C++23)

      [edit]Usage

      The floating-point conversion rank and subrank are also used to

      (since C++23)

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1642C++98usual arithmetic conversions might involve lvaluesapplies lvalue-to-rvalue conversions first
      CWG 2528C++20the three-way comparison betweenunsignedchar
      andunsignedint is ill-formed because
      of the intermediate integral promotion[1]
      determines the common type based
      on the promoted types, without
      actually promoting the operands[2]
      CWG 2892C++98when both operands are of the same
      floating-point type, the meaning of “no
      further conversion is needed” was unclear
      changed to “no further
      conversion will be performed”
      1. Before the resolution,unsignedchar is promoted toint at the beginning of stage 5, then it is converted tounsignedint. However, the latter conversion is narrowing, which makes the three-way comparison ill-formed.
      2. After the resolution, the common type is stillunsignedint. The difference is thatunsignedchar is directly converted tounsignedint without the intermediate integral promotion. The conversion is not narrowing and hence the three-way comparison is well-formed.
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/usual_arithmetic_conversions&oldid=175310"

      [8]ページ先頭

      ©2009-2025 Movatter.jp