Namensräume
Varianten

Move constructors

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.
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.
Standardkonstruktor
Copy-Konstruktor
bewegen Konstruktor(C++11)
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
 
Ein Umzug Konstruktor der Klasse T ist eine Non-template Konstruktor, dessen erste Parameter ist T&&, const T&&, volatile T&& oder constvolatile T&&, und entweder gibt es keine weiteren Parameter oder der Rest der alle Parameter Standardwerte haben. Ein Typ mit einem öffentlichen Zug Konstruktor ist MoveConstructible .
Original:
A move constructor of class T is a non-template constructor whose first parameter is T&&, const T&&, volatile T&&, or constvolatile T&&, and either there are no other parameters, or the rest of the parameters all have default values. A type with a public move constructor is MoveConstructible.
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

class_name ( class_name&& ) (1) (seit C++11)
class_name ( class_name&& ) = default; (2) (seit C++11)
class_name ( class_name&& ) = delete; (3) (seit C++11)

[Bearbeiten]Erklärung

# Typische Deklaration einer Bewegung Konstruktor
Original:
# Typical declaration of a move constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Erzwingen ein Umzug Konstruktor vom Compiler erzeugt werden
Original:
# Forcing a move constructor to be generated by the compiler
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
# Vermeiden impliziten move Konstruktor
Original:
# Avoiding implicit move constructor
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Der Schritt Konstruktor aufgerufen wird, wenn ein Objekt aus xWert des gleichen Typs, die umfasst initialisiert wird
Original:
The move constructor is called whenever an object is initialized from xvalue of the same type, which includes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Initialisierung T a = std::move(b); oder T a(std::move(b));, wo b vom Typ T ist
    Original:
    initialization, T a = std::move(b); or T a(std::move(b));, where b is of type T
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Funktion Argumentübergabe: f(std::move(a));, wo a der Typ ist T und f ist void f(T t)
    Original:
    function argument passing: f(std::move(a));, where a is of type T and f is void f(T t)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Return: return a; innerhalb einer Funktion wie T f(), wo a ist vom Typ T die einen Umzug Konstruktor hat .
    Original:
    function return: return a; inside a function such as T f(), where a is of type T which has a move constructor.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Bewegen Konstruktoren typischerweise "stehlen" die Ressourcen mit dem Argument (zB Zeiger auf dynamisch zugewiesenen Objekte, Dateideskriptoren TCP-Sockets, I / O-Streams, laufenden Threads, etc), anstatt Kopien von ihnen hielt, und lassen Sie das Argument in Einige gültige aber ansonsten unbestimmten Zustand. Zum Beispiel, sich von einer std::string oder von einem std::vector wird das Argument leer .
Original:
Move constructors typically "steal" the resources held by the argument (e.g. pointers to dynamically-allocated objects, file descriptors, TCP sockets, I/O streams, running threads, etc), rather than make copies of them, and leave the argument in some valid but otherwise indeterminate state. For example, moving from a std::string or from a std::vector turns the argument empty.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten]Implizit deklarierte Bewegung Konstruktor

Wenn keine benutzerdefinierten move Konstruktoren für eine Klasse-Typ (struct, class oder union) und werden, sofern alle der folgenden Bedingungen erfüllt ist:
Original:
If no user-defined move constructors are provided for a class type (struct, class, or union), and all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Es gibt noch keine deklarierte Kopie Konstruktoren
    Original:
    there are no user-declared copy constructors
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Es gibt keine vom Anwender deklariert Kopie Zuweisungsoperatoren
    Original:
    there are no user-declared copy assignment operators
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Es gibt keine vom Anwender deklariert move Zuweisungsoperatoren
    Original:
    there are no user-declared move assignment operators
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Es gibt noch keine deklarierte destructurs
    Original:
    there are no user-declared destructurs
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • die implizit deklarierte Bewegung Konstruktor nicht definiert als gelöscht
    Original:
    the implicitly-declared move constructor would not be defined as deleted
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
dann wird der Compiler einen Umzug Konstruktor als inline public Mitglied seiner Klasse mit der Unterschrift T::T(T&&) erklären
Original:
then the compiler will declare a move constructor as an inline public member of its class with the signature T::T(T&&)
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Eine Klasse kann mehrere Konstruktoren bewegen, z. B. sowohl T::T(const T&&) und T::T(T&&). Wenn einige benutzerdefinierte move Konstruktoren vorhanden sind, kann der Benutzer noch zwingen die Erzeugung des implizit deklarierten move Konstruktor mit dem Schlüsselwort default .
Original:
A class can have multiple move constructors, e.g. both T::T(const T&&) and T::T(T&&). If some user-defined move constructors are present, the user may still force the generation of the implicitly declared move constructor with the keyword default.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten]Gelöschte implizit deklariert move Konstruktor

Die implizit deklariert oder ausgefallen move Konstruktor für die Klasse T ist definiert als gelöscht in einer der folgenden Punkte zutrifft:
Original:
The implicitly-declared or defaulted move constructor for class T is defined as deleted in any of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T hat nicht-statische Datenelemente, die nicht verschoben (gelöscht haben, unzugänglich oder zweideutigen Bewegung Konstruktoren) werden können
    Original:
    T has non-static data members that cannot be moved (have deleted, inaccessible, or ambiguous move constructors)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T unmittelbar oder virtuelle Basisklasse, die nicht verschoben werden können (gelöscht hat, unzugänglich oder mehrdeutig move Konstruktoren) werden
    Original:
    T has direct or virtual base class that cannot be moved (has deleted, inaccessible, or ambiguous move constructors)
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T unmittelbar oder virtuelle Basisklasse mit einer gelöschten oder unzugänglichen destructor
    Original:
    T has direct or virtual base class with a deleted or inaccessible destructor
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T eine benutzerdefinierte move Konstruktor oder unterwegs Zuweisungsoperator
    Original:
    T has a user-defined move constructor or move assignment operator
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T ist ein Zusammenschluss und hat eine Variante Mitglied nicht-triviale Copy-Konstruktor
    Original:
    T is a union and has a variant member with non-trivial copy constructor
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T hat eine nicht-statische Daten Mitglied oder eine direkte oder virtuelle Basis, ohne eine Bewegung Konstruktor, der nicht trivial kopierbar .
    Original:
    T has a non-static data member or a direct or virtual base without a move constructor that is not trivially copyable.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten]Trivial move Konstruktor

Die implizit deklariert move Konstruktor für die Klasse T ist trivial, wenn alle der folgenden Bedingungen erfüllt ist:
Original:
The implicitly-declared move constructor for class T is trivial if all of the following is true:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • T hat keine virtuelle Member-Funktionen
    Original:
    T has no virtual member functions
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • T hat keine virtuellen Basisklassen
    Original:
    T has 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 Umzug Konstruktor für jede direkte Basis von T gewählt ist trivial
    Original:
    The move constructor selected for every direct base of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • Der Umzug Konstruktor für jedes nicht statische Klasse-Typ (oder ein Array von Klasse-Typ) memeber der T gewählt ist trivial
    Original:
    The move constructor selected for every non-static class type (or array of class type) memeber of T is trivial
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
Eine triviale move Konstruktor ist ein Konstruktor, der die gleiche Wirkung wie der trivialen Copykonstruktor, also macht, ist eine Kopie der Objekt-Repräsentation wie von std::memmove führt. Alle Datentypen kompatibel mit der Programmiersprache C (POD-Typen) sind trivial beweglichen .
Original:
A trivial move constructor is a constructor that performs the same action as the trivial copy constructor, that is, makes a copy of the object representation as if by std::memmove. All data types compatible with the C language (POD types) are trivially movable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten]Zug-Konstruktor implizit definiert

Wenn die implizit deklariert move Konstruktor nicht gelöscht oder trivial, ist es definiert (das heißt, eine Funktion Körper erzeugt und kompiliert) durch den Compiler. Für union Typen, kopiert die implizit definierte Bewegung Konstruktor die Objekt-Repräsentation (wie std::memmove). Für Nicht-union-Klasse-Typen (class und struct), führt der Umzug Konstruktor Vollmitglied-kluger Schachzug des Objekts Basen und nicht-statische Mitglieder, in deren Initialisierung Reihenfolge mit direkten Initialisierung mit einem xWert Argument .
Original:
If the implicitly-declared move constructor is not deleted or trivial, it is defined (that is, a function body is generated and compiled) by the compiler. For union types, the implicitly-defined move constructor copies the object representation (as by std::memmove). For non-union class types (class and struct), the move constructor performs full member-wise move of the object's bases and non-static members, in their initialization order, using direct initialization with an xvalue argument.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[Bearbeiten]Notes

Um starke Ausnahme Garantie möglich, sollten benutzerdefinierte move Konstruktoren keine Ausnahmen. In der Tat, Standard-Container typischerweise auf std::move_if_noexcept zwischen Verschieben und Kopieren wählen, wenn Container-Elemente verlegt werden müssen .
Original:
To make strong exception guarantee possible, user-defined move constructors should not throw exceptions. In fact, standard containers typically rely on std::move_if_noexcept to choose between move and copy when container elements need to be relocated.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Wenn beide kopieren und verschieben Konstruktoren versehen sind, wählt Überladungsauflösung den Umzug Konstruktor, wenn das Argument ein rvalue (entweder prvalue wie eine namenlose temporäre oder xWert, wie das Ergebnis der std::move) und wählt den Copy-Konstruktor, wenn das Argument lvalue (benannte Objekt oder eine Funktion / Betreiber wieder lvalue Referenz). Wenn nur die Kopie Konstruktor bereitgestellt wird, alle Argumente Kategorien es auszuwählen (solange sie Bezug nimmt, um const, da rvalues ​​const Referenzen binden können), was das Kopieren der Fallback zu bewegen, ist beim Bewegen verfügbar .
Original:
If both copy and move constructors are provided, overload resolution selects the move constructor if the argument is an rvalue (either prvalue such as a nameless temporary or xvalue such as the result of std::move), and selects the copy constructor if the argument is lvalue (named object or a function/operator returning lvalue reference). If only the copy constructor is provided, all argument categories select it (as long as it takes reference to const, since rvalues can bind to const references), which makes copying the fallback for moving, when moving is unavailable.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
In vielen Situationen, Verschieben Konstruktoren durchgeführt werden, selbst wenn sie beobachtbaren Nebenwirkungen erzeugen würde optimiert finden Kopie elision
Original:
In many situations, move constructors are optimized out even if they would produce observable side-effects, see Kopie elision
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 <string>#include <iostream>   struct A {std::string s; A(): s("test"){} A(const A& o): s(o.s){std::cout<<"move failed!\n";} A(A&& o): s(std::move(o.s)){}};   A f(A a){return a;}   struct B : A {std::string s2;int n;// implicit move-contructor B::(B&&)// calls A's move constructor// calls s2's move constructor// and makes a bitwise copy of n};   struct C : B { ~C(){};// destructor prevents implicit move};   struct D : B { D(){} ~D(){};// destructor would prevent implicit move D(D&&)=default;// force a move ctor anyway};   int main(){std::cout<<"Trying to move A\n"; A a1 = f(A());// move-construct from rvalue temporary A a2 = std::move(a1);// move-construct from xvalue   std::cout<<"Trying to move B\n"; B b1;std::cout<<"Before move, b1.s = \""<< b1.s<<"\"\n"; B b2 = std::move(b1);// calls implicit move ctorstd::cout<<"After move, b1.s = \""<< b1.s<<"\"\n";   std::cout<<"Trying to move C\n"; C c1; C c2 = std::move(c1);// calls the copy constructor   std::cout<<"Trying to move D\n"; D d1; D d2 = std::move(d1);}

Output:

Trying to move A Trying to move B Before move, b1.s = "test" After move, b1.s = "" Trying to move C move failed! Trying to move D
close