Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      Expressions

      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
       

      An expression is a sequence ofoperators and theiroperands, that specifies a computation.

      Expression evaluation may produce a result (e.g., evaluation of2+2 produces the result4) and may generate side-effects (e.g. evaluation ofstd::printf("%d",4) prints the character'4' on the standard output).

      Each C++ expression is characterized by two independent properties: A type and a value category.

      Contents

      [edit]General

      • value categories (lvalue, rvalue, glvalue, prvalue, xvalue(since C++11)) classify expressions by their values
      • order of evaluation of arguments and subexpressions specify the order in which intermediate results are obtained

      [edit]Operators

      Common operators
      assignmentincrement
      decrement
      arithmeticlogicalcomparisonmember
      access
      other

      a= b
      a+= b
      a-= b
      a*= b
      a/= b
      a%= b
      a&= b
      a|= b
      a^= b
      a<<= b
      a>>= b

      ++a
      --a
      a++
      a--

      +a
      -a
      a+ b
      a- b
      a* b
      a/ b
      a% b
      ~a
      a& b
      a| b
      a^ b
      a<< b
      a>> b

      !a
      a&& b
      a|| b

      a== b
      a!= b
      a< b
      a> b
      a<= b
      a>= b
      a<=> b

      a[...]
      *a
      &a
      a->b
      a.b
      a->*b
      a.*b

      function call

      a(...)
      comma

      a, b
      conditional

      a? b: c
      Special operators

      static_cast converts one type to another related type
      dynamic_cast converts within inheritance hierarchies
      const_cast adds or removescv-qualifiers
      reinterpret_cast converts type to unrelated type
      C-style cast converts one type to another by a mix ofstatic_cast,const_cast, andreinterpret_cast
      new creates objects with dynamic storage duration
      delete destructs objects previously created by the new expression and releases obtained memory area
      sizeof queries the size of a type
      sizeof... queries the size of apack(since C++11)
      typeid queries the type information of a type
      noexcept checks if an expression can throw an exception(since C++11)
      alignof queries alignment requirements of a type(since C++11)

      [edit]Conversions

      [edit]Memory allocation

      [edit]Other

      [edit]Primary expressions

      The operands of any operator may be other expressions or primary expressions (e.g. in1+2*3, the operands of operator+ are thesubexpression2*3 and the primary expression1).

      Primary expressions are any of the following:

      (since C++26)
      (since C++11)
      (since C++17)
      (since C++20)

      Any expression in parentheses is also classified as a primary expression: this guarantees that the parentheses have higher precedence than any operator. Parentheses preserve value, type, and value category.

      [edit]Literals

      Literals are the tokens of a C++ program that represent constant values embedded in the source code.

      • char orwchar_t
      • char16_t orchar32_t
      (since C++11)
      • char8_t
      (since C++20)
      • constchar[] orconstwchar_t[]
      • constchar16_t[] orconstchar32_t[]
      (since C++11)
      • const char8_t[]
      (since C++20)
      (since C++11)

      [edit]Full-expressions

      Aconstituent expression is defined as follows:

      • The constituent expression of an expression is that expression.
      • The constituent expressions of abrace-enclosed initializer list or of a (possibly parenthesized) expression list are the constituent expressions of the elements of the respective list.
      • The constituent expressions of aninitializer that begins with= are the constituent expressions of theinitializer-clause.
      int num1=0;num1+=1;// Case 1: the constituent expression of “num += 1” is “num += 1” int arr2[2]={2,22}// Case 2: the constituent expressions//         of “{2, 22}” are “2” and “22”// Case 3: the constituent expressions of “= {2, 22}”//         are the constituent expressions of “{2, 22}”//         (i.e. also “2” and “22”)

      Theimmediate subexpressions of an expressionE are

      • the constituent expressions ofE’s operands,
      (since C++14)
      • ifE is alambda expression, the initialization of the entities captured by copy and the constituent expressions of the initializer of the captures,
      (since C++11)
      • any function call thatE implicitly invokes, or
      • ifE is a function call or implicitly invokes a function, the constituent expressions of eachdefault argument used in the call.

      Asubexpression of an expressionE is an immediate subexpression ofE or a subexpression of an immediate subexpression ofE.Note that expressions appearing in the “function body” of lambda expressions are not subexpressions of the lambda expression.(since C++11)

      The following expressions arefull-expressions :

      (since C++20)
      (since C++26)
      • expressions that are not a subexpression of any another expression and that are not otherwise part of any full-expression

      If a language construct is defined to produce an implicit call of a function, a use of the language construct is considered to be an expression for the purposes of this definition. Conversions applied to the result of an expression in order to satisfy the requirements of the language construct in which the expression appears are also considered to be part of the full-expression.

      For an initializer, performing the initialization of the entity(including evaluating default member initializers of an aggregate)(since C++14) is also considered part of the full-expression.

      [edit]Potentially-evaluated expressions

      An expression ispotentially evaluated unless

      • it is the operand of thesizeof operator, or
      • it is the operand of thetypeid operator and does not designate an lvalue ofpolymorphic class type.
      (until C++11)

      The following operands areunevaluated operands, they are not evaluated:

      • expressions which thetypeid operator applies to, except glvalues ofpolymorphic class types
      • expressions which are operands of thesizeof operator
      • operands of thenoexcept operator
      • operands of thedecltype specifier
      (since C++20)

      An expression ispotentially evaluated unless

      • it is an unevaluated operand, or
      • it is a subexpression of an unevaluated operand.
      (since C++11)

      Potentially-evaluated expressions areODR-use.

      This section is incomplete
      Reason: example of unevaluated operands

      [edit]Discarded-value expressions

      Adiscarded-value expression is an expression that is used for its side-effects only. The value calculated from such expression is discarded. Such expressions include the full-expression of anyexpression statement, the left-hand operand of the built-in comma operator, or the operand of a cast-expression that casts to the typevoid.

      Array-to-pointer and function-to-pointer conversions are never applied to the value calculated by a discarded-value expression. The lvalue-to-rvalue conversion is applied if and only if the expression is avolatile-qualified glvalue and has one of the following forms (built-in meaning required, possibly parenthesized):

      • id-expression,
      • array subscript expression,
      • class member access expression,
      • indirection,
      • pointer-to-member operation,
      • conditional expression where both the second and the third operands are one of these expressions,
      • comma expression where the right operand is one of these expressions.

      In addition, if the lvalue is of volatile-qualified class type, a volatile copy constructor is required to initialize the resulting rvalue temporary.

      If the expression is a non-void prvalue (after any lvalue-to-rvalue conversion that might have taken place),temporary materialization occurs.

      Compilers may issue warnings when an expression other than cast tovoid discards a value declared[[nodiscard]].

      (since C++17)

      Expression-equivalence

      A number of expressionse1,e2, ...,eN areexpression-equivalent if all following conditions are satisfied:

      1. They have the same effects.
      2. Either they are allconstant subexpressions or neither is.
      3. Either they are allnoexcept or else neither is.

      e1 isexpression-equivalent toe2 if and only ife1 ande2 are expression-equivalent (which meanse2 is also expression-equivalent toe1).

      (since C++20)

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      CWG 1054C++98assigning a value to a volatile variable might
      result in an unnecessary read due to the lvalue-to-
      rvalue conversion applied to the assignment result
      introduce discarded-value expressions
      and exclude this case from the list
      of cases that require the conversion
      CWG 1343C++98sequencing of destructor calls in
      aggregate initialization was underspecified
      full-expressions in aggregate initialization
      are well-specified
      CWG 1383C++98the list of expressions where lvalue-to-rvalue
      conversion is applied to discarded-value
      expressions also covered overloaded operators
      only cover operators
      with built-in meaning
      CWG 1576C++11lvalue-to-rvalue conversions were not applied
      to discarded-value volatile xvalue expressions
      apply the conversion
      in this case
      CWG 2249C++98identifiers to be declared in declarators
      were not id-expressions
      they are
      CWG 2431C++11the invocations of the destructors of temporaries that
      are bound to references were not full-expressions
      they are

      [edit]See also

      C documentation forExpressions
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/expressions&oldid=181350"

      [8]ページ先頭

      ©2009-2025 Movatter.jp