Namensräume
Varianten
Aktionen

list initialization

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
 
Initialisiert ein Objekt aus braced-init-Liste
Original:
Initializes an object from braced-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Inhaltsverzeichnis

[Bearbeiten] Syntax

T object { arg1, arg2, ... }; (1)
T { arg1, arg2, ... }; (2)
new T { arg1, arg2, ... }; (3)
return { arg1, arg2, ... } ; (4)
function( { arg1, arg2, ... } ) ; (5)
object[ { arg1, arg2, ... } ] ; (6)
T( { arg1, arg2, ... } ) (7)
Class { T member = { arg1, arg2, ... }; }; (8)
Class::Class() : member{arg1, arg2, ...} {... (9)
T object = {arg1, arg2, ...}; (10)

[Bearbeiten] Erklärung

Liste der Initialisierung wird in den folgenden Situationen ausgeführt:
Original:
List initialization is performed in the following situations:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
Initialisierung einer benannten Variable mit einem Doppelpack eingeschlossenen Liste von Ausdrücken oder verschachtelte Listen (verspannt-init-Liste)
Original:
initialization of a named variable with a brace-enclosed list of expressions or nested lists (braced-init-list)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
Initialisierung eines ungenannten temporären mit einer verspannt-init-Liste
Original:
initialization of an unnamed temporary with a braced-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
Initialisierung eines Objekts mit dynamischen Lagerdauer mit einem neuen Ausdruck, wo die Initialisierung ist eine Klammer-init-Liste
Original:
initialization of an object with dynamic storage duration with a new-expression, where the initializer is a brace-init-list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
in einem return Anweisung mit braced-init-Liste als return-Ausdruck verwendet
Original:
in a return statement with braced-init-list used as the return expression
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
in einem Aufruf der Funktion Ausdruck, mit verspannten-init-list als Argument verwendet
Original:
in a function call expression, with braced-init-list used as an argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
in einem Index Ausdruck mit einem benutzerdefinierten Operator []
Original:
in a subscript expression with a user-defined operator[]
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
7)
in einem funktionalen Cast-Ausdruck oder andere direkte Initialisierung, mit verspannten-init-Liste als Konstruktorargument verwendet
Original:
in a functional cast expression or other direct-initialization, with braced-init-list used as the constructor argument
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
8)
in einer nicht-statische Daten Mitglied Initialisierung
Original:
in a non-static data member initializer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
9)
in einem Konstruktor Initialisierungsliste
Original:
in a constructor initializer list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
10)
auf der rechten-Hand-Seite des Gleichheitszeichens (ähnlich copy-Initialisierung)
Original:
on the right-hand-side of the equals sign (similar to copy-Initialisierung)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Die Wirkungen der Liste Initialisierung eines Objekts vom Typ T sind:
Original:
The effects of list initialization of an object of type T are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Wenn die braced-init-Liste ist leer und T ist eine Klasse-Typ mit einem Standardkonstruktor wird Value-Initialisierung durchgeführt .
    Original:
    If the braced-init-list is empty and T is a class type with a default constructor, Value-Initialisierung is performed.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Andernfalls, wenn T ist eine Spezialisierung der std::initializer_list eine neue std::initializer_list Objekt des gleichen Typs gebaut und eingesetzt zu lenken initialisieren oder kopieren initialisieren das Objekt vom Typ T, abhängig vom Kontext .
    Original:
    Otherwise, if T is a specialization of std::initializer_list, a new std::initializer_list object of the same type is constructed and used to direct-initialize or copy-initialize the object of type T, depending on context.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Andernfalls werden die constuctors der T betrachtet, in zwei Phasen:
    Original:
    Otherwise, the constuctors of T are considered, in two phases:
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Alle Konstruktoren, std::initializer_list nehmen als einziges Argument oder als erstes Argument, wenn die restlichen Argumente Standardwerte haben, werden geprüft und abgestimmt durch Überlastung Resolution gegen ein einzelnes Argument vom Typ std::initializer_list
    Original:
    All constructors that take std::initializer_list as the only argument, or as the first argument if the remaining arguments have default values, are examined, and matched by overload resolution against a single argument of type std::initializer_list
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Wenn der vorherigen Stufe nicht produziert ein Spiel, alle Konstruktoren T in Überlast Beschlussfassung teilnehmen gegen die Reihe von Argumenten, die aus den Elementen der braced-init-Liste besteht, mit der Einschränkung, dass nur einschränkende Konvertierungen erlaubt sind. Wenn dieses Stadium produziert einen expliziten Konstruktor als beste Übereinstimmung für eine Kopie-list-Initialisierung, Erstellung fehlschlägt (beachten Sie, in einfachen Copy-Initialisierung werden explizite Konstruktoren überhaupt nicht berücksichtigt)
    Original:
    If the previous stage does not produce a match, all constructors of T participate in overload resolution against the set of arguments that consists of the elements of the braced-init-list, with the restriction that only narrowing conversions are allowed. If this stage produces an explicit constructor as the best match for a copy-list-initialization, compilation fails (note, in simple copy-initialization, explicit constructors are not considered at all)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Andernfalls, wenn T Referenz-Typ ist, ist ein prvalue vorübergehende des referenzierten Typs list-initialisiert, und der Verweis auf diese temporären gebunden .
    Original:
    Otherwise, if T is reference type, a prvalue temporary of the referenced type is list-initialized, and the reference is bound to that temporary.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Andernfalls, wenn die verspannt-init-Liste hat nur ein Element, T ist direkte initialisiert oder copy-initialisiert, abhängig vom Kontext, außer dass einschränkende Konvertierungen sind nicht erlaubt .
    Original:
    Otherwise, if the braced-init-list has only one element, T is direkte initialisiert or copy-initialisiert, depending on context, except that narrowing conversions are not allowed.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Andernfalls, wenn die verspannt-init-Liste keine Elemente, T ist Wert initialisiert .
    Original:
    Otherwise, if the braced-init-list has no elements, T is Wert initialisiert.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Einschränkende Konvertierungen

list-Initialisierung begrenzt die zulässige impliziten Konvertierungen durch das Verbot der folgenden:
Original:
list-initialization limits the allowed impliziten Konvertierungen by prohibiting the following:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Umwandlung von einer Floating-Point-Typ in einen Integer-Typ
    Original:
    conversion from a floating-point type to an integer type
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Umwandlung von einer long double um double oder float und Umwandlung von double um float, sofern nicht die Quelle ist ein konstanter Ausdruck dessen Wert genau in den Ziel-Typ gespeichert werden
    Original:
    conversion from a long double to double or to float and conversion from double to float, except where the source is a constant expression whose value can be stored exactly in the target type
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Umwandlung von ganzzahligen oder ohne Bereichseinschränkung Enumerationstyp um Ganzzahl, die sich nicht darstellen können alle Werte des ursprünglichen, sofern Quelle ist ein konstanter Ausdruck dessen Wert genau in den Ziel-Typ gespeichert werden
    Original:
    conversion from integer or unscoped enumeration type to integer type that cannot represent all values of the original, except where source is a constant expression whose value can be stored exactly in the target type
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Notes

Verspannt-init-Liste ist nicht ein Ausdruck und hat keinen Typ für sich: zum Beispiel beim Aufruf einer Funktion Vorlage verspannt-init-list Argument kann nicht für Vorlagentyp Abzug verwendet werden. Eine besondere Ausnahme für das Schlüsselwort auto, die jede verspannt-init-Liste als std::initializer_list leitet gemacht .
Original:
Braced-init-list is not an expression and has no type on its own: for example, when calling a function template, braced-init-list argument cannot be used for template type deduction. A special exception is made for the keyword auto, which deduces any braced-init-list as std::initializer_list.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten] Beispiel

#include <iostream>
#include <vector>
#include <map>
#include <string>
 
struct Foo {
    std::vector<int> mem = {1,2,3}; // list-initialization of a non-static member
    std::vector<int> mem2;
    Foo() : mem2{-1, -2, -3} {} // list-initialization of a member in constructor
};
 
std::pair<std::string, std::string> f(std::pair<std::string, std::string> p)
{
    return {p.second, p.first}; // list-initialization in return statement
}
 
int main()
{
    int n0{};     // value-initialization (to zero)
    int n1{1};    // direct-list-initialization
    std::string s1{'a', 'b', 'c', 'd'}; // initializer-list constructor call
    std::string s2{s1, 2, 2};           // regular constructor call
    std::string s3{0x61, 'a'}; // initializer-list ctor is preferred to (int, char)
 
    int n2 = {1}; // copy-list-initialization
    double d = double{1.2}; // list-initialization of a temporary, then copy-init
 
    std::map<int, std::string> m = { // nested list-initialization
           {1, "a"},
           {2, {'a', 'b', 'c'} },
           {3, s1}
    };
 
    std::cout << f({"hello", "world"}).first // list-initialization in function call
              << '\n';
 
    const int (&ar)[2] = {1,2}; // binds a lvalue reference to a temporary array
    int&& r1 = {1}; // binds a rvalue reference to a temporary int
//  int& r2 = {2}; // error: cannot bind rvalue to a non-const lvalue ref
 
//  int bad{1.0}; // error: narrowing conversion
    unsigned char uc1{10}; // okay
//  unsigned char uc2{-1}; // error: narrowing conversion
 
    Foo f;
 
    std::cout << n0 << ' ' << n1 << ' ' << n2 << '\n'
              << s1 << ' ' << s2 << ' ' << s3 << '\n';
    for(auto p: m)
        std::cout << p.first << ' ' << p.second << '\n';
    for(auto n: f.mem)
        std::cout << n << ' ';
    for(auto n: f.mem2)
        std::cout << n << ' ';
}

Output:

world
0 1 1
abcd cd aa
1 a
2 abc
3 abcd
1 2 3 -1 -2 -3


[Bearbeiten] Siehe auch