Namespaces
Variants

std::tuple<Types...>:: operator=

From cppreference.net
Utilities library
tuple & operator = ( const tuple & other ) ;
(1) (C++11부터)
(C++20부터 constexpr)
constexpr const tuple & operator = ( const tuple & other ) const ;
(2) (C++23부터)
tuple & operator = ( tuple && other ) noexcept ( /* see below */ ) ;
(3) (C++11부터)
(C++20부터 constexpr)
constexpr const tuple & operator = ( tuple && other ) const ;
(4) (C++23 이후)
template < class ... UTypes >
tuple & operator = ( const tuple < UTypes... > & other ) ;
(5) (C++11 이후)
(C++20 이후 constexpr)
template < class ... UTypes >
constexpr const tuple & operator = ( const tuple < UTypes... > & other ) const ;
(6) (C++23부터)
template < class ... UTypes >
tuple & operator = ( tuple < UTypes... > && other ) ;
(7) (C++11부터)
(C++20부터 constexpr)
template < class ... UTypes >
constexpr const tuple & operator = ( tuple < UTypes... > && other ) const ;
(8) (C++23 이후)
template < class E1, class E2 >
tuple & operator = ( const std:: pair < E1, E2 > & p ) ;
(9) (C++11부터)
(C++20부터 constexpr)
template < class E1, class E2 >
constexpr const tuple & operator = ( const std:: pair < E1, E2 > & p ) const ;
(10) (C++23 이후)
template < class E1, class E2 >
tuple & operator = ( std:: pair < E1, E2 > && p ) ;
(11) (C++11 이후)
(C++20 이후 constexpr)
template < class E1, class E2 >
constexpr const tuple & operator = ( std:: pair < E1, E2 > && p ) const ;
(12) (C++23 이후)
template < tuple - like UTuple >
constexpr tuple & operator = ( UTuple && u ) ;
(13) (C++23 이후)
template < tuple - like UTuple >
constexpr const tuple & operator = ( UTuple && u ) const ;
(14) (C++23부터)

튜플의 내용을 다른 튜플과 유사한 객체의 내용으로 대체합니다.

다음 설명에서 다음과 같이 가정합니다.

  • i 가 범위 [ 0 , sizeof... ( Types ) ) 내에서 순서대로 존재하고,
  • Ti 가 클래스 템플릿 매개변수 팩 Types i 번째 타입이며,
  • Ui UTypes 라는 이름의 함수 템플릿 매개변수 팩에서 i 번째 타입인 경우,

인덱싱은 0부터 시작합니다.

1) 복사 할당 연산자. other 의 각 요소를 * this 의 해당 요소에 할당합니다.
이 오버로드는 모든 Ti 에 대해 std:: is_copy_assignable < Ti > :: value true 인 경우를 제외하고 삭제된 것으로 정의됩니다.
2) const로 한정된 피연산자에 대한 복사 할당 연산자. other 의 각 요소를 * this 의 해당 요소에 할당합니다.
이 오버로드는 모든 Ti 에 대해 std:: is_copy_assignable_v < const Ti > true 인 경우에만 오버로드 해결에 참여합니다.
3) 이동 할당 연산자. 모든 i 에 대해, std:: forward < Ti > ( std :: get < i > ( other ) ) std :: get < i > ( * this ) 에 할당합니다.
이 오버로드는 모든 Ti 에 대해 std:: is_move_assignable < Ti > :: value true 인 경우에만 오버로드 해결에 참여합니다.
4) const 한정 피연산자에 대한 이동 할당 연산자. 모든 i 에 대해, std:: forward < Ti > ( std :: get < i > ( other ) ) std :: get < i > ( * this ) 에 할당합니다.
이 오버로드는 모든 Ti 에 대해 std:: is_assignable_v < const Ti & , Ti > true 인 경우에만 오버로드 해결에 참여합니다.
5) 모든 i 에 대해, std :: get < i > ( other ) std :: get < i > ( * this ) 에 할당합니다.
이 오버로드는 다음 조건에서만 오버로드 해결에 참여합니다: sizeof... ( Types ) == sizeof... ( UTypes ) 이고, std:: is_assignable < Ti & , const Ui & > :: value 가 모든 해당 타입 쌍 Ti Ui 에 대해 true 인 경우입니다.
6) 모든 i 에 대해, std :: get < i > ( other ) std :: get < i > ( * this ) 에 할당합니다.
이 오버로드는 다음 조건이 충족될 때만 오버로드 해결에 참여합니다: sizeof... ( Types ) == sizeof... ( UTypes ) , 그리고 모든 해당 타입 쌍 Ti Ui 에 대해 std:: is_assignable_v < const Ti & , const Ui & > true 인 경우.
7) 모든 i 에 대해, std:: forward < Ui > ( std :: get < i > ( other ) ) std :: get < i > ( * this ) 에 할당합니다.
이 오버로드는 다음 조건에서만 오버로드 해결에 참여합니다: sizeof... ( Types ) == sizeof... ( UTypes ) , 그리고 모든 해당 타입 쌍 Ti Ui 에 대해 std:: is_assignable < Ti & , Ui > :: value true 인 경우.
8) 모든 i 에 대해, std:: forward < Ui > ( std :: get < i > ( other ) ) std :: get < i > ( * this ) 에 할당합니다.
이 오버로드는 다음 조건에서만 오버로드 해결에 참여합니다: sizeof... ( Types ) == sizeof... ( UTypes ) , 그리고 모든 해당 타입 쌍 Ti Ui 에 대해 std:: is_assignable_v < const Ti & , Ui > true 인 경우.
9) p. first * this 의 첫 번째 요소에 할당하고, p. second * this 의 두 번째 요소에 할당합니다.
이 오버로드는 다음 조건에서만 오버로드 해결에 참여합니다:
10) p. first 에 첫 번째 요소를 할당하고 p. second 에 두 번째 요소를 할당합니다.
이 오버로드는 다음 조건이 모두 충족될 때만 오버로드 해결에 참여합니다:
11) std:: forward < E1 > ( p. first ) * this 의 첫 번째 요소에 할당하고, std:: forward < E2 > ( p. second ) * this 의 두 번째 요소에 할당합니다.
이 오버로드는 다음 조건을 모두 만족할 때만 오버로드 해결에 참여합니다:
12) 첫 번째 요소에 std:: forward < E1 > ( p. first ) 를 할당하고, 두 번째 요소에 std:: forward < E2 > ( p. second ) 를 할당합니다.
이 오버로드는 다음 조건을 모두 만족할 때만 오버로드 해결에 참여합니다:
13) 모든 i 에 대해, std :: get < i > ( std:: forward < UTuple > ( u ) ) std :: get < i > ( * this ) 에 할당합니다.
이 오버로드는 다음 조건을 모두 만족할 때만 오버로드 해결에 참여합니다:
14) 모든 i 에 대해, std :: get < i > ( std:: forward < UTuple > ( u ) ) std :: get < i > ( * this ) 에 할당합니다.
이 오버로드는 다음 조건을 모두 만족할 때만 오버로드 해결에 참여합니다:

목차

매개변수

other - 이 튜플의 내용을 대체할 튜플
p - 이 2-튜플의 내용을 대체할 페어
u - tuple-like 이 튜플의 내용을 대체할 튜플-라이크 객체

반환값

* this

예외

1,2) Types 에 포함된 타입들 중 하나의 할당이 예외를 발생시키는 경우, 구현에서 정의된 예외를 발생시킬 수 있습니다.
3)
noexcept 명세:
noexcept (

std:: is_nothrow_move_assignable < T0 > :: value &&
std:: is_nothrow_move_assignable < T1 > :: value &&
std:: is_nothrow_move_assignable < T2 > :: value &&
...

)
4-14) Types 에 포함된 타입 중 하나의 할당이 예외를 발생시키는 경우, 구현에서 정의된 예외를 발생시킬 수 있습니다.

예제

#include <iostream>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
// std::vector<int> 출력을 위한 헬퍼 함수
std::ostream& operator<<(std::ostream& os, std::vector<int> const& v)
{
    os << '{';
    for (std::size_t t = 0; t != v.size(); ++t)
        os << v[t] << (t + 1 < v.size() ? ", " : "");
    return os << '}';
}
// 임의 크기의 튜플을 출력하는 헬퍼 함수
template<class... Args>
void print_tuple(std::string_view name, const std::tuple<Args...>& t)
{
    std::cout << name << " = {";
    std::apply([&](auto&& arg, auto&&... args)
    {
        std::cout << arg;
        ((std::cout << ", " << args), ...);
    }, t);
    std::cout << '}';
}
template<class Tuple1, class Tuple2>
void print_tuples(std::string_view name1, const Tuple1& t1,
                  std::string_view name2, const Tuple2& t2)
{
    print_tuple(name1, t1);
    std::cout << ", ";
    print_tuple(name2, std::tuple(t2));
    std::cout << "\n\n";
}
int main()
{
    // 튜플에서 튜플로의 변환 예제 //
    std::tuple<int, std::string, std::vector<int>>
        t1{1, "알파", {1, 2, 3}},
        t2{2, "베타", {4, 5}};
    print_tuples("1) t1", t1, "t2", t2);
    // 일반 복사 할당
    // operator=( const tuple& other );
    t1 = t2;
    print_tuples("2) t1 = t2;\n   t1", t1, "t2", t2);
    // Normal move assignment
    // operator=( tuple&& other );
    t1 = std::move(t2);
    print_tuples("3) t1 = std::move(t2);\n   t1", t1, "t2", t2);
    // 복사 할당 변환
    // operator=( const tuple<UTypes...>& other );
    std::tuple<short, const char*, std::vector<int>> t3{3, "감마", {6, 7, 8}};
    t1 = t3;
    print_tuples("4) t1 = t3;\n   t1", t1, "t3", t3);
    // 이동 할당 변환
    // operator=( tuple&& other );
    t1 = std::move(t3);
    print_tuples("5) t1 = std::move(t3);\n   t1", t1, "t3", t3);
    // Pair to tuple 예제 //
    std::tuple<std::string, std::vector<int>> t4{"델타", {10, 11, 12}};
    std::pair<const char*, std::vector<int>> p1{"엡실론", {14, 15, 16}};
    print_tuples("6) t4", t4, "p1", p1);
    // std::pair에서 복사 할당 변환
    // operator=( const std::pair<U1, U2>& p );
    t4 = p1;
    print_tuples("7) t4 = p1;\n   t4", t4, "p1", p1);
    // std::pair에서 이동 할당 변환
    // operator=( std::pair<U1, U2>&& p );
    t4 = std::move(p1);
    print_tuples("8) t4 = std::move(p1);\n   t4", t4, "p1", p1);
}

가능한 출력:

1) t1 = {1, alpha, {1, 2, 3}}, t2 = {2, beta, {4, 5}}
2) t1 = t2;
   t1 = {2, beta, {4, 5}}, t2 = {2, beta, {4, 5}}
3) t1 = std::move(t2);
   t1 = {2, beta, {4, 5}}, t2 = {2, , {}}
4) t1 = t3;
   t1 = {3, gamma, {6, 7, 8}}, t3 = {3, gamma, {6, 7, 8}}
5) t1 = std::move(t3);
   t1 = {3, gamma, {6, 7, 8}}, t3 = {3, gamma, {}}
6) t4 = {delta, {10, 11, 12}}, p1 = {epsilon, {14, 15, 16}}
7) t4 = p1;
   t4 = {epsilon, {14, 15, 16}}, p1 = {epsilon, {14, 15, 16}}
8) t4 = std::move(p1);
   t4 = {epsilon, {14, 15, 16}}, p1 = {epsilon, {}}

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 2729 C++11 operator = 제약 조건이 없어 불필요한
미정의 동작이 발생할 수 있었음
제약 조건 추가됨

참고 항목

새로운 tuple 을 생성합니다
(public member function)
내용을 할당합니다
( std::pair<T1,T2> 의 public member function)