C++17: Keep only some members when tuple unpacking

前端 未结 4 1233
囚心锁ツ
囚心锁ツ 2020-12-25 10:23

Let\'s imagine you need to call the following method:

std::tuple foo();

In C++17, you can call the function and unpack

4条回答
  •  既然无缘
    2020-12-25 11:03

    MSVC has already fixed this in VS 15.7 Preview. The final 15.7 release should be available in the coming weeks. This means that the current logic supported by the latest releases of all major compilers is as follows:

    • If at least one of the structured bindings in a structured binding declaration is used, no "Unused variable" warning will be issued for other bindings in the same declaration.
    • If none of the bindings in a structured binding declaration are used, it is possible to silence the warning by using the [[maybe_unused]] attribute:

      [[maybe_unused]] auto [a, b, c] = foo();

提交回复
热议问题