constexpr specifier (depuis C++11)
De cppreference.com
![]() | This page has been machine-translated from the English version of the wiki using Google Translate. The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
constexpr
- indique que la valeur d'une variable ou une fonction peut être calculée à la compilationOriginal:constexpr
- specifies that the value of a variable or function can be computed at compile timeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Explication
constexpr
spécificateur déclare qu'il est possible d'évaluer la valeur de la fonction ou variable au moment de la compilation, ils peuvent ensuite être utilisés lorsque que le temps de compiler expressions constantes sont autorisés. constexpr implique const .Original:
constexpr
specifier declares that it is possible to evaluate the value of the function or variable at compile time, they then can be used where only compile time expressions constantes are allowed. constexpr implies const.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
'constexpr les variables' doit satisfaire aux exigences suivantes:
Original:
constexpr variables must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- il doit être immédiatement construit ou assigné une valeur .Original:it must be immediately constructed or assigned a value.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - le constructeur paramètres ou la valeur à attribuer doit contenir uniquement des valeurs littérales, des variables et des fonctions
constexpr
.Original:the constructor parameters or the value to be assigned must contain only literal values,constexpr
variables and functions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - le constructeur utilisé pour construire l'objet (implicite ou explicite) doit satisfaire aux exigences de constructeur
constexpr
. Dans le cas de constructeur explicite, il doit avoir constexpr spécifié .Original:the constructor used to construct the object (either implicit or explicit) must satisfy the requirements ofconstexpr
constructor. In the case of explicit constructor, it must have constexpr specified.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
'constexpr fonctions' doit satisfaire aux exigences suivantes:
Original:
constexpr functions must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- il ne doit pas être virtuelleOriginal:it must not be virtualThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - son type de retour doit être
LiteralType
Original:its return type must beLiteralType
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - chacun de ses paramètres doivent être de type littéralOriginal:each of its parameters must be literal typeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - le corps de la fonction doit être soit supprimée, soit par défaut ou contiennent seulement ce qui suit:Original:the function body must be either deleted or defaulted or contain only the following:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- déclarations nullesOriginal:null statementsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - static_assert déclarationsOriginal:static_assert declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. typedef
déclarations et déclarations alias qui ne définissent pas des classes ou des énumérationsOriginal:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.- l'utilisation de déclarationsOriginal:using declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - en utilisant les directivesOriginal:using directivesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - exactement une déclaration
return
qui contient des valeurs littérales, des variables queconstexpr
et fonctions .Original:exactly onereturn
statement that contains only literal values,constexpr
variables and functions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
'constexpr constructeur' doit satisfaire aux exigences suivantes:
Original:
constexpr constructor must satisfy the following requirements:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
- chacun de ses paramètres doivent être de type littéralOriginal:each of its parameters must be literal typeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - la classe ne doit pas avoir de classes de base virtuellesOriginal:the class must have no virtual base classesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - le corps du constructeur doit être soit supprimée, soit par défaut ou contiennent seulement ce qui suit:Original:the constructor body must be either deleted or defaulted or contain only the following:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- déclarations nullesOriginal:null statementsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - static_assert déclarationsOriginal:static_assert declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. typedef
déclarations et déclarations alias qui ne définissent pas des classes ou des énumérationsOriginal:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.- l'utilisation de déclarationsOriginal:using declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - en utilisant les directivesOriginal:using directivesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- le constructeur ne doit pas avoir un bloc de fonction-tryOriginal:the constructor must not have a function-try blockThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - chaque classe de base et chaque élément non-statique doit être initialisé, soit dans le
initializer_list
ou par initialisation entretoise-ou-égal. Terminer chaque constructeur concerné doit être un constructeur constexpr et chacune des clauses de chaque initialiseur accolades ou égal doit être une expression constanteOriginal:every base class and every non-static member must be initialized, either in theinitializer_list
or by brace-or-equal initializer. End every constructor involved must be a constexpr constructor and every clause of every brace-or-equal initializer must be a constant expressionThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - chaque conversion implicite en question doit être une expression constanteOriginal:every implicit conversion involved must be a constant expressionThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[modifier]Mots-clés
[modifier]Exemple
Définition d'une fonction qui calcule constexpr factorielles et un type littéral qui s'étend des chaînes littérales
Original:
Definition of a constexpr function which computes factorials and a literal type that extends string literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
#include <iostream>#include <stdexcept> // constexpr functions use recursion rather than iterationconstexprint factorial(int n){return n <=1?1:(n * factorial(n-1));} // literal classclass conststr {constchar* p;std::size_t sz;public:template<std::size_t N>constexpr conststr(constchar(&a)[N]): p(a), sz(N-1){}// constexpr functions signal errors by throwing exceptions from operator ?:constexprchar operator[](std::size_t n){return n < sz ? p[n]:throwstd::out_of_range("");}constexprstd::size_t size(){return sz;}};constexprstd::size_t countlower(conststr s, std::size_t n =0, std::size_t c =0){return n == s.size()? c : s[n]>='a'&& s[n]<='z'? countlower(s, n+1, c+1): countlower(s, n+1, c);} // output function that requires a compile-time constant, for testingtemplate<int n>struct constN { constN(){std::cout<< n <<'\n';}}; int main(){std::cout<<"4! = "; constN<factorial(4)> out1;// computed at compile time volatileint k =8;std::cout<< k <<"! = "<< factorial(k)<<'\n';// computed at run time std::cout<<"Number of lowercase letters in \"Hello, world!\" is "; constN<countlower("Hello, world!")> out2;// implicitly converted to conststr}
Résultat :
4! = 24 8! = 40320 Number of lowercase letters in "Hello, world!" is 9