Expressions [En cours de Traduction]
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 |
+a |
!a |
a == b |
a[b] |
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) |
- operator precedence définie l'odre dans lequel les opérateurs sont liés à leurs aguments
- alternative representations sont des autre appelations pour certains operateurs
- operator overloading rends possible de spécifier le comportement d'un opérateur avec des classes utilisateur.
[modifier] Conversions
- standard conversions implicit conversions from one type to another
-
const_cast
conversion -
static_cast
conversion -
dynamic_cast
conversion -
reinterpret_cast
conversion - explicit cast conversion using C-style cast notation and functional notation
- user-defined conversion makes it possible to specify conversion from user-defined classes
[modifier] Memory allocation
- new expression allocates memory dynamically
- delete expression deallocates memory dynamically
[modifier] Other
- constant expressions can be evaluated at compile time and used in compile-time context (template arguments, array sizes, etc)
-
sizeof
-
alignof
-
typeid
- throw-expression
[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:
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.
- integer literals are decimal, octal, hexadecimal or binary numbers of integer type.
- character literals are individual characters of type
- char or wchar_t
- char16_t or char32_t (depuis C++11)
- char8_t (depuis C++20)
- floating-point literals are values of type float, double, or long double
- string literals are sequences of characters of type
- 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.
[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.