简体   繁体   中英

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();
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM