Search code examples
c++tuplesc++17

C++17 Tuple Unpacking


I am trying to unpack a tuple and use the results to assign into members. Is it possible to do that in idiomatic C++17?

I realize that std::tie exists but I am trying to utilize C++17 features and not default to older features nor the old way (std::get<N>(tuple))

tuple<A, vector<A>> IO(){
     //IO happens here
     return {var, vec};
}

class foo{
public:
    foo();
private:
    A var;
    vector<A> vec;
};

foo::foo(){
    //this line here: I want to assign var and vec from IO()
    [var, vec] = IO();
}

Solution

  • Not really. Structured bindings can only declare new names, it cannot assign to existing ones.

    Best would be to just do this:

    foo() : foo(IO()) { } // delegate to a constructor, calling IO
    
    foo(std::tuple<A, vector<A>>&& tup) // manually unpack within this constructor
      : var(std::get<0>(std::move(tup)))
      , vec(std::get<1>(std::move(tup)))
    { }
    

    If A happens to be default constructible and move assignable, then yeah this works too:

    foo() {
        std::tie(var, vec) = IO();
    }
    

    If A happens to not be default constructible, then you could use optional to add that extra state:

    struct foo {
        std::optional<A> var;
        std::vector<A> vec;
    
        foo() {
            std::tie(var, vec) = IO();
        }
    };
    

    None of these are particularly great.