2

My question is

What part of the cppreference.com's page on structured binding declarations should make apparent that they cannot be used with "things" not known at compile time?

That page does not contain any explicit reference to compile or run time.

Why?

I recently bumped into this question about whether structured binding declarations work with std::vector, which doesn't since its size is not known at compile time. Since I'm trying to understand how Boost.Hana works, I need to make clear in my mind what the detailed distinction between run time and compile time is, hence I took the chance to ask the question above.

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • The "compile-time" was a bit of simplification. `std::array` goes into Case 2, `std::vector` would go into Case 3, but it doesn't have any public members to bind. – Yksisarvinen Aug 18 '20 at 07:58
  • 1
    Of interest: [Runtime vs. Compile time](https://stackoverflow.com/q/846103/6865932) – anastaciu Aug 18 '20 at 08:08
  • @Yksisarvinen, since _Case 1: if E is an array type, then the names are bound to the array elements_, but you say `std::array` goes into Case 2, I guess I'm a bit confused. Maybe you can help further? – Enlico Aug 18 '20 at 08:27
  • 2
    @EnricoMariaDeAngelis "array type" = C-style array. `std::tuple_size` has [a specialization for `std::array`](https://en.cppreference.com/w/cpp/container/array/tuple_size), so it goes to the second case. – Yksisarvinen Aug 18 '20 at 08:33
  • @Yksisarvinen, I feel stupid now for not thinking about the C-style array. Thanks, and also thanks for the link. – Enlico Aug 18 '20 at 08:53

1 Answers1

6

The relevant part is std::tuple_size, which yields a compile-time size. You can't implement that for std::vector. It has a run-time variable std::vector::size.

MSalters
  • 173,980
  • 10
  • 155
  • 350