program tip

std :: tie는 어떻게 작동합니까?

radiobox 2020. 8. 18. 07:32
반응형

std :: tie는 어떻게 작동합니까?


나는 그것에 std::tie대해 많이 생각하지 않고 사용 했습니다. 작동하므로 다음을 수락했습니다.

auto test()
{
   int a, b;
   std::tie(a, b) = std::make_tuple(2, 3);
   // a is now 2, b is now 3
   return a + b; // 5
}

하지만이 흑 마법 은 어떻게 작동합니까? 어떻게 일시적인가 만든 않습니다 std::tie변화 ab? 나는 이것이 언어 기능이 아니라 라이브러리 기능이기 때문에 더 흥미 롭다는 것을 알게되었고, 확실히 우리가 스스로 구현하고 이해할 수있는 것입니다.


핵심 개념을 명확히하기 위해 좀 더 기본적인 예를 들어 보겠습니다. std::tie더 많은 값 (튜플)을 반환하는 함수에 유용 하지만 하나의 값만으로도 잘 이해할 수 있습니다.

int a;
std::tie(a) = std::make_tuple(24);
return a; // 24

앞으로 나아 가기 위해 알아야 할 사항 :

  • std::tie 참조의 튜플을 구성하고 반환합니다.
  • std::tuple<int>그리고 std::tuple<int&>그들은 같은 템플릿에서 생성 된 것을 다른 이들 사이에 연결이 완전히 다른 클래스는,,,이다 std::tuple.
  • 튜플은 operator=서로 다른 유형 (하지만 동일한 번호)의 튜플을 받아들이고 각 멤버는 cppreference 에서 개별적으로 할당됩니다 .

    template< class... UTypes >
    tuple& operator=( const tuple<UTypes...>& other );
    

    (3) 모든 i에 std::get<i>(other)대해 std::get<i>(*this).

다음 단계는 방해가되는 함수를 제거하는 것이므로 코드를 다음과 같이 변환 할 수 있습니다.

int a;
std::tuple<int&>{a} = std::tuple<int>{24};
return a; // 24

다음 단계는 해당 구조 내부에서 일어나는 일을 정확히 확인하는 것입니다. 이를 위해 두 가지 유형의 T치환기 std::tuple<int>Tr치환기 std::tuple<int&>를 생성하고 작업을 위해 최소한으로 줄였습니다.

struct T { // substituent for std::tuple<int>
    int x;
};

struct Tr { // substituent for std::tuple<int&>
    int& xr;

    auto operator=(const T& other)
    {
       // std::get<I>(*this) = std::get<I>(other);
       xr = other.x;
    }
};

auto foo()
{
    int a;
    Tr{a} = T{24};

    return a; // 24
}

And finally, I like to get rid of the structures all together (well, it's not 100% equivalent, but it's close enough for us, and explicit enough to allow it):

auto foo()
{
    int a;

    { // block substituent for temporary variables

    // Tr{a}
    int& tr_xr = a;

    // T{24}
    int t_x = 24;

    // = (asignement)
    tr_xr = t_x;
    }

    return a; // 24
}

So basically, std::tie(a) initializes a data member reference to a. std::tuple<int>(24) creates a data member with value 24, and the assignment assigns 24 to the data member reference in the first structure. But since that data member is a reference bound to a, that basically assigns 24 to a.


This does not answer your question in any way, but let me post it anyway because C++17 is basically ready (with compiler support), so while wondering how the outdated stuff works, it is probably worth looking at how the current, and future, version of C++ works, too.

With C++17 you can pretty much scratch std::tie in favour of what is called structured bindings. They do the same (well, not the same, but they have the same net effect), although you need to type fewer characters, it does not need library support, and you also have the ability to take references, if that happens to be what you want.

(Note that in C++17 constructors do argument deduction, so make_tuple has become somewhat superfluous, too.)

int a, b;
std::tie(a, b) = std::make_tuple(2, 3);

// C++17
auto  [c, d] = std::make_tuple(4, 5);
auto  [e, f] = std::tuple(6, 7);
std::tuple t(8,9); auto& [g, h] = t; // not possible with std::tie

참고URL : https://stackoverflow.com/questions/43762651/how-does-stdtie-work

반응형