◐ Shell
clean mode source ↗

constexpr specifier (dal C++11) - cppreference.com

Da cppreference.com.

Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.

La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui.

Click here for the English version of this page

<metanoindex/>

  • constexpr - specifica che il valore di una variabile o di una funzione può essere calcolata in fase di compilazione

    Original:

    constexpr - specifies that the value of a variable or function can be computed at compile time

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Spiegazione

constexpr identificatore dichiara che è possibile valutare il valore della funzione o variabile al momento della compilazione, che quindi possono essere utilizzati dove solo il tempo di compilazione sono ammessi espressioni costanti. 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 espressioni costanti 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.

'constexpr variabili' deve soddisfare i seguenti requisiti:

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.

  • deve essere immediatamente costruito o assegnato un valore.

    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.

  • il costruttore parametri o il valore da assegnare deve contenere solo valori letterali, variabili constexpr e funzioni.

    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.

  • il costruttore utilizzato per costruire l'oggetto (sia implicita o esplicita) devono soddisfare i requisiti di costruzione constexpr. Nel caso del costruttore esplicito, deve essere specificato constexpr.

    Original:

    the constructor used to construct the object (either implicit or explicit) must satisfy the requirements of constexpr 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 funzioni' deve soddisfare i seguenti requisiti:

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.

  • non deve essere virtuale

    Original:

    it must not be virtual

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • il tipo restituito deve essere LiteralType

    Original:

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • ciascuno dei suoi parametri devono essere di tipo letterale

    Original:

    each of its parameters must be literal type

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • il corpo della funzione deve essere eliminato o inadempienti o reca esclusivamente le seguenti:

    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.

  • istruzioni nulle

    Original:

    null statements

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • static_assert dichiarazioni

    Original:

    static_assert declarations

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • typedef dichiarazioni e le dichiarazioni alias che non definiscono classi o enumerazioni

    Original:

    typedef declarations and alias declarations that do not define classes or enumerations

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • utilizzare dichiarazioni

    Original:

    using declarations

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • l'uso di direttive

    Original:

    using directives

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • esattamente una dichiarazione return che contiene solo valori letterali, variabili constexpr e funzioni.

    Original:

    exactly one return 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 costruttore' devono soddisfare i seguenti requisiti:

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.

  • ciascuno dei suoi parametri devono essere di tipo letterale

    Original:

    each of its parameters must be literal type

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • la classe deve avere classi base virtuali

    Original:

    the class must have no virtual base classes

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • il corpo del costruttore deve essere eliminato o inadempienti o reca esclusivamente le seguenti:

    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.

  • istruzioni nulle

    Original:

    null statements

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • static_assert dichiarazioni

    Original:

    static_assert declarations

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • typedef dichiarazioni e le dichiarazioni alias che non definiscono classi o enumerazioni

    Original:

    typedef declarations and alias declarations that do not define classes or enumerations

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • utilizzare dichiarazioni

    Original:

    using declarations

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • l'uso di direttive

    Original:

    using directives

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • il costruttore non deve avere una funzione-blocco try

    Original:

    the constructor must not have a function-try block

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • ogni classe di base e di ogni membro non statico deve essere inizializzato, sia nella initializer_list o inizializzatore coppia-o-uguale. Terminare ogni costruttore coinvolto deve essere un costruttore constexpr e ogni clausola di ogni inizializzatore coppia-o-uguale deve essere un'espressione costante

    Original:

    every base class and every non-static member must be initialized, either in the initializer_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 expression

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

  • ogni conversione implicita coinvolto deve essere un'espressione costante

    Original:

    every implicit conversion involved must be a constant expression

    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

Parole chiave

constexpr

Esempio

Definizione di una funzione che calcola constexpr fattoriali e un tipo letterale che si estende stringhe letterali

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.

#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