Espaces de noms
Variantes
Affichages
Actions

Expressions [En cours de Traduction]

De cppreference.com
< cpp‎ | language

Modèle:cpp/language/expressions/navbar An expression is a sequence of operators and their operands, qui spécifie un calcul. Une expression est une séquence d' "opérateurs" et d' "opérandes", qui spécifie une opération.

Une évaluation peut produire un résultat (e.g., evaluation de 2+2 produit le résultat 4) et peut générer des effets secondaires (e.g. evaluation de std::printf("%d",4) affiche le caractère '4' dans la sortie standard).

Sommaire

[modifier] General

  • value categories (lvalue, rvalue, glvalue, prvalue, xvalue) classifie les arguments par leurs valeurs
  • order of evaluation d'arguments et de sous-expressions qui spécifie l'ordre dans lequel les résultat intermédaires sont obtenues.

[modifier] Operateurs

Opérateurs ordinaires
affectation incrémentation
décrémentation
arithmétique logique comparaison accès aux membre autre

a = b
a = rvalue
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->b
a.b
a->*b
a.*b

a(...)
a, b
(type) a
? :

Opérateurs spéciaux

static_cast convertit un type dans un autre type compatible

dynamic_cast convertit une classe de base virtuelle dans une classe dérivée

const_cast convertit un type dans un type compatible avec des cv-qualifiers différents

reinterpret_cast convertit un type dans un type incompatibles

new allocation de la mémoire

delete libère de la mémoire

sizeof récupère la taille d'un type

sizeof... récupère la taille d'un paquet de paramètres (depuis C++11)

typeid récupère les informations de type d'un type

noexcept vérifie si une expression peut lancer une exception (depuis C++11)

alignof récupère les conditions d'alignement d'un type (depuis C++11)

[modifier] Conversions

[modifier] Memory allocation

[modifier] Other


[modifier] Primary expressions

The operands of any operator may be other expressions or primary expressions (e.g. in 1+2*3, the operands of operator+ are the subexpression 2*3 and the primary expression 1).

Primary expressions are any of the following:

1) Literals (e.g. 2 or "Hello, world")
2) Suitably declared unqualified identifiers (e.g. n or cout)
3) Suitably declared qualified identifiers (e.g. std::string::npos)
5) Fold-expressions (C++17)

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.

[modifier] Literals

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

  • char or wchar_t
  • char16_t or char32_t (depuis C++11)
  • char8_t (depuis C++20)
  • const char[] or const wchar_t[]
  • const char16_t[] or const char32_t[] (depuis C++11)
  • const char8_t[] (depuis C++20)
  • boolean literals are values of type bool, that is true and false
  • nullptr is the pointer literal which specifies a null pointer value (depuis C++11)
  • user-defined literals are constant values of user-specified type (depuis C++11)

[modifier] Unevaluated expressions

The operands of the operators typeid, sizeof, noexcept, and decltype (depuis C++11) are expressions that are not evaluated (unless they are polymorphic glvalues and are the operands of typeid), since these operators only query the compile-time properties of their operands. Thus, std::size_t n = sizeof(std::cout << 42); does not perform console output.

Modèle:rrev

Modèle:rrev

[modifier] Discarded-value expressions

A discarded-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 any expression statement, the left-hand operand of the built-in comma operator, or the operand of a cast-expression that casts to the type void.

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 a volatile-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.

Modèle:rrev