Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does structured binding pack allow empty pack from zero tuple-sized object?

Tags:

c++

c++26

Structured bindings do not allow empty decomposable types.

auto [] = std::make_tuple(); // error

Ever since P1061R10 has been accepted for C++26, this allows structured bindings to introduce packs (as long as the pack is declared within template context):

auto [...args] = return_empty_tuple();
auto [one, ...rest] = return_single_tuple();

The latter allows ...rest to be an empty pack if return_single_tuple() has a structured binding size of 1.

My question is, can ...args still be declared as an empty pack even if return_empty_tuple() has a zero structured binding size (e.g. zero tuple size)?

like image 844
Desmond Gold Avatar asked Jun 21 '26 13:06

Desmond Gold


1 Answers

P1061R10 mentions "empty" twice, but doesn't answer your question directly and the latest C++26 draft doesn't include the wording for P1061 yet, but, I dare to say Yes.

In P1061, the authors show many examples of how letting structured bindings introduce packs simplifies generic code where std::apply must be used today, sometimes in multiple layers. Even the implementation of std::apply itself could be simplified to

template <class F, class Tuple>
constexpr decltype(auto) apply(F &&f, Tuple &&t) {
    auto&& [...elems] = t;
    return std::invoke(std::forward<F>(f),
                       forward_like<Tuple, decltype(elems)>(elems)...);
}

according to the proposal. If empty packs will not be allowed, this implementation of std::apply would not work, and neither would many other situations in generic code.

Therefore, it's without question that the authors meant for empty packs to be allowed.

(Also, the experimental implementation of P1061 in Clang accepts them: Demo)

like image 167
Ted Lyngmo Avatar answered Jun 23 '26 06:06

Ted Lyngmo