不可能与现有变量进行结构化绑定?

时间:2017-06-26 12:19:49

标签: c++ c++17 structured-bindings

是否可以将现有的变量用作与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
}

1 个答案:

答案 0 :(得分:6)

如果您想使用现有变量,那么您可以std::tie

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

结构化绑定引入了新标识符。不幸的是,对于一般聚合,没有任何内容等同于std::tie