Namensräume
Varianten
Aktionen

constexpr specifier (seit C++11)

Aus cppreference.com
< cpp‎ | language

 
 
Sprache C + +
Allgemeine Themen
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Flusskontrolle
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Bedingte Ausführung Aussagen
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Iterationsanweisungen
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Gehe Aussagen
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktionen
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funktion Erklärung
Lambda-Funktion Erklärung
Funktions-Template
inline-Spezifizierer
Exception-Spezifikationen (veraltet)
noexcept Spezifizierer (C++11)
Ausnahmen
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Namespaces
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Specifiers
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv Planer
Lagerdauer Planer
constexpr Spezifizierer (C++11)
auto Spezifizierer (C++11)
alignas Spezifizierer (C++11)
Initialisierung
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Literale
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expressions
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
alternative Darstellungen
Utilities
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Types
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
Typ Aliasdeklaration (C++11)
Attribute (C++11)
Wirft
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
impliziten Konvertierungen
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-Stil und funktionale Besetzung
Speicherzuweisung
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Classes
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Class-spezifische Funktion Eigenschaften
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
explizit (C++11)
statisch
Besondere Member-Funktionen
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Templates
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Klassen-Template
Funktions-Template
Template-Spezialisierung
Parameter Packs (C++11)
Verschiedenes
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Inline Montage
 
  • constexpr - legt fest, dass der Wert einer Variable oder Funktion zur Kompilierzeit können berechnet werden
    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.

[Bearbeiten] Erklärung

constexpr Spezifizierer erklärt, dass es möglich ist, den Wert der Funktion oder Variable zur Kompilierzeit auszuwerten, sie kann dann verwendet werden, wo nur Kompilierungszeit konstante Ausdrücke erlaubt sind. constexpr bedeutet 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 konstante Ausdrücke 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 Variablen' muss die folgenden Anforderungen erfüllen:
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.
  • Es muss sofort gebaut werden oder ein Wert zugewiesen .
    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.
  • der Konstruktor Parameter oder der Wert zugewiesen werden darf nur literalen Werten, constexpr Variablen und Funktionen .
    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.
  • der Konstruktor verwendet, um das Objekt (entweder implizit oder explizit) konstruieren müssen den Anforderungen der constexpr Konstruktor. Im Falle der expliziten Konstruktor, muss es constexpr angegeben haben .
    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 Funktionen' muss die folgenden Anforderungen erfüllen:
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.
  • es darf nicht virtuell
    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.
  • seine Rückkehr Typ muss LiteralType sein
    Original:
    its return type must be LiteralType
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • jeder seiner Parameter müssen wörtliche Typ sein
    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.
  • die Funktion Körper muss entweder gelöscht oder ausgefallen oder enthalten nur die folgenden:
    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.
  • null Aussagen
    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 Erklärungen
    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 Erklärungen und Alias Erklärungen, die nicht definieren Klassen oder Aufzählungen
    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.
  • mit Erklärungen
    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.
  • using-Direktiven
    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.
  • genau ein return Anweisung, die nur literalen Werten, constexpr Variablen und Funktionen enthält .
    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 Konstruktor' muss folgenden Anforderungen genügen:
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.
  • jeder seiner Parameter müssen wörtliche Typ sein
    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.
  • Die Klasse muss keine virtuellen Basisklassen
    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.
  • der Konstruktor Körper muss entweder gelöscht oder ausgefallen oder enthalten nur die folgenden:
    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.
  • null Aussagen
    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 Erklärungen
    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 Erklärungen und Alias Erklärungen, die nicht definieren Klassen oder Aufzählungen
    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.
  • mit Erklärungen
    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.
  • using-Direktiven
    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.
  • Der Konstruktor muss keine Funktion-try-Block
    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.
  • jede Basisklasse und jeder Nicht-statisches Element muss initialisiert werden, entweder in der initializer_list oder durch Klammer-oder-gleich-Initialisierung. Ende jeder Bauherr beteiligt sind, müssen eine constexpr Konstruktor und jede Klausel jeder Klammer-oder-gleich initializer sein muss ein konstanter Ausdruck sein
    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.
  • jede implizite Konvertierung einbezogen muss ein konstanter Ausdruck sein
    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.

[Bearbeiten] Keywords

constexpr

[Bearbeiten] Beispiel

Definition eines constexpr Funktion, die Fakultäten und eine buchstäbliche Art, die Zeichenfolgenliterale erstreckt berechnet

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