constexpr specifier (ya que 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- especifica que el valor de una variable o función puede ser calculada en tiempo de compilaciónOriginal: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.
[editar] Explicación
constexpr especificador declara que no es posible evaluar el valor de la función o variable en tiempo de compilación, que luego se puede usar donde sólo el tiempo de compilación expresiones constantes se permiten. constexpr implica 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 expresiones 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 las variables' debe cumplir los siguientes requisitos:
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.
- debe ser inmediatamente construido o asigna un valor .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. - el constructor de parámetros o el valor que se asignará debe contener sólo valores literales, variables y funciones
constexpr.Original:the constructor parameters or the value to be assigned must contain only literal values,constexprvariables and functions.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - el constructor utilizado para construir el objeto (ya sea implícito o explícito) debe satisfacer los requisitos de constructor
constexpr. En el caso de constructor explícito, debe haber especificado constexpr .Original:the constructor used to construct the object (either implicit or explicit) must satisfy the requirements ofconstexprconstructor. 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 funciones' debe cumplir los siguientes requisitos:
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.
- no debe ser virtualOriginal: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. - su tipo de retorno debe ser
LiteralTypeOriginal:its return type must beLiteralTypeThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - cada uno de sus parámetros deben ser de tipo literalOriginal: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. - el cuerpo de la función debe ser eliminado o cesación de pagos o sólo contienen lo siguiente: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.
- declaraciones nulasOriginal: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 declaracionesOriginal:static_assert declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
typedefdeclaraciones y declaraciones alias que no definen las clases o enumeracionesOriginal:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - mediante declaracionesOriginal:using declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - mediante directivasOriginal:using directivesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - exactamente una declaración
returnque contiene sólo valores literales, variables y funcionesconstexpr.Original:exactly onereturnstatement that contains only literal values,constexprvariables 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 constructor' deberán cumplir los requisitos siguientes:
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.
- cada uno de sus parámetros deben ser de tipo literalOriginal: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 clase no debe tener clases base virtualesOriginal: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. - el cuerpo del constructor debe ser eliminado o cesación de pagos o sólo contienen lo siguiente: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.
- declaraciones nulasOriginal: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 declaracionesOriginal:static_assert declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. -
typedefdeclaraciones y declaraciones alias que no definen las clases o enumeracionesOriginal:The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - mediante declaracionesOriginal:using declarationsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - mediante directivasOriginal:using directivesThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
-
- el constructor no debe tener un bloque de función-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. - cada clase base y cada miembro no estático debe ser inicializado, ya sea en el
initializer_listo inicializador llave-o-igual. Termine cada constructor en cuestión debe ser un constructor constexpr y todas las cláusulas de cada inicializador llave-o-igual debe ser una expresión constanteOriginal:every base class and every non-static member must be initialized, either in theinitializer_listor 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. - cada conversión implícita en cuestión debe ser una expresión 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.
-
[editar] Palabras clave
[editar] Ejemplo
Definición de una función que calcula factoriales constexpr y un tipo literal que se extiende literales de cadena
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 iteration constexpr int factorial(int n) { return n <= 1 ? 1 : (n * factorial(n-1)); } // literal class class conststr { const char * p; std::size_t sz; public: template<std::size_t N> constexpr conststr(const char(&a)[N]) : p(a), sz(N-1) {} // constexpr functions signal errors by throwing exceptions from operator ?: constexpr char operator[](std::size_t n) { return n < sz ? p[n] : throw std::out_of_range(""); } constexpr std::size_t size() { return sz; } }; constexpr std::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 testing template<int n> struct constN { constN() { std::cout << n << '\n'; } }; int main() { std::cout << "4! = " ; constN<factorial(4)> out1; // computed at compile time volatile int 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 }
Output:
4! = 24 8! = 40320 Number of lowercase letters in "Hello, world!" is 9