Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

structured binding with existing vars not possible?

Is it possible to use already existing vars as target for return values in connection with structured bindings?

auto f()
{
    return std::make_tuple(1,2.2);
}

int main()
{
    int x;
    double z;

    [ x, z ] = f();  // Did not work in gcc 7.1

    // structured bindings only work with "new" vars?
    auto [a, b] = f();  // works fine
}
like image 724
Klaus Avatar asked Jun 26 '17 12:06

Klaus


Video Answer


1 Answers

If you want to use existing variables, you have std::tie for that purpose.

std::tie(x, z) = f(); // only works with tuples however

Structured bindings introduce new identifiers. Unfortunately there is nothing equivalent to std::tie for general aggregates.

like image 81
StoryTeller - Unslander Monica Avatar answered Nov 14 '22 22:11

StoryTeller - Unslander Monica