Namensräume
Varianten
Aktionen

std::inplace_vector<T,N>::operator=

Von cppreference.com
 
 
 
 
constexpr inplace_vector& operator=( const inplace_vector& other );
(1) (seit C++26)
constexpr inplace_vector& operator=( inplace_vector&& other )
    noexcept(/* siehe unten */);
(2) (seit C++26)
constexpr inplace_vector& operator=( std::initializer_list<T> init );
(3) (seit C++26)

Ersetzt den Inhalt von inplace_vector.

1) Zuweisungsoperator für Kopie. Ebenfalls ein trivialer Zuweisungsoperator für Kopie, wenn std::inplace_vector<T, N> einen trivialen Destruktor hat und std::is_trivially_copy_constructible_v<T> && std::is_trivially_copy_assignable_v<T> wahr ist. Ersetzt den Inhalt durch eine Kopie des Inhalts von other.
2) Zuweisungsoperator für Verschiebung. Ebenfalls ein trivialer Zuweisungsoperator für Verschiebung, wenn std::inplace_vector<T, N> einen trivialen Destruktor hat und std::is_trivially_move_constructible_v<T> && std::is_trivially_move_assignable_v<T> wahr ist. Ersetzt den Inhalt durch den Inhalt von other unter Verwendung von Verschiebesemantik (d. h. die Daten in other werden von other in diesen Container verschoben). other befindet sich danach in einem gültigen, aber undefinierten Zustand.
3) Ersetzt den Inhalt durch die durch die Initialisierungsliste init identifizierten Elemente.

Inhalt

[edit] Parameter

Sonstiges - ein weiterer inplace_vector, der als Quelle zur Initialisierung der Elemente des Containers verwendet wird
init - Initialisierungsliste zum Initialisieren der Elemente des Containers

[edit] Komplexität

1,2) Linear zur Größe von *this und other.
3) Linear zur Größe von *this und init.

[edit] Ausnahmen

2)
noexcept-Spezifikation:  
noexcept(N = 0 ||

        (std::is_nothrow_move_assignable_v<T> &&

         std::is_nothrow_move_constructible_v<T>))
3) Wirft std::bad_alloc, wenn init.size() > N.

[edit] Beispiel

#include <initializer_list>
#include <inplace_vector>
#include <new>
#include <print>
#include <ranges>
#include <string>
 
int main()
{
    std::inplace_vector<int, 4> x({1, 2, 3}), y;
    std::println("Initially:");
    std::println("x = {}", x);
    std::println("y = {}", y);
 
    std::println("Copy assignment copies data from x to y:");
    y = x; // overload (1)
    std::println("x = {}", x);
    std::println("y = {}", y);
 
    std::inplace_vector<std::string, 3> z, w{"\N{CAT}", "\N{GREEN HEART}"};
    std::println("Initially:");
    std::println("z = {}", z);
    std::println("w = {}", w);
 
    std::println("Move assignment moves data from w to z:");
    z = std::move(w); // overload (2)
    std::println("z = {}", z);
    std::println("w = {}", w); // w is in valid but unspecified state
 
    auto l = {4, 5, 6, 7};
    std::println("Assignment of initializer_list {} to x:", l);
    x = l; // overload (3)
    std::println("x = {}", x);
 
    std::println("Assignment of initializer_list with size bigger than N throws:");
    try
    {
        x = {1, 2, 3, 4, 5}; // throws: (initializer list size == 5) > (capacity N == 4)
    }
    catch(const std::bad_alloc& ex)
    {
        std::println("ex.what(): {}", ex.what());
    }
}

Mögliche Ausgabe

Initially:
x = [1, 2, 3]
y = []
Copy assignment copies data from x to y:
x = [1, 2, 3]
y = [1, 2, 3]
Initially:
z = []
w = ["🐈", "💚"]
Move assignment moves data from w to z:
z = ["🐈", "💚"]
w = ["", ""]
Assignment of initializer_list [4, 5, 6, 7] to x:
x = [4, 5, 6, 7]
Assignment of initializer_list with size bigger than N throws:
ex.what(): std::bad_alloc

[edit] Siehe auch

konstruiert den inplace_vector
(public member function) [edit]
weist dem Container Werte zu
(public member function) [edit]