Why does including break structured bindings in GCC?

后端 未结 2 952
滥情空心
滥情空心 2021-02-18 15:04

Consider:

struct Point { int x, y; };

int main()
{
    const auto [x, y] = Point{};
}

This code compiles fine with gcc 7.1 in C++17 mode, howe

2条回答
  •  抹茶落季
    2021-02-18 16:09

    The core idea behind structured bindings is that std::tuple_size defines how many components you get from unpacking T, and T::get should access the N'th element. Not surprising, this std::tuple_size is a specialization from the base template in .

    Now in this case, Point doesn't have such support for structured bindings, but it is a special case (all public non-static members) for which C++17 states that no special unpacking support is needed. This is an exception to the rule above.

    The compiler is tripping over itself here, and trying to use the generic rule when it sees the unspecialized std::tuple_size from .

提交回复
热议问题