Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do structured bindings not support variadics?

C++17 adds structured bindings:

tuple<int, int, int> make_tuple();
int test() {
    auto [a, b, c] = make_tuple();
    return a | b | c;
}

My immediate instinct was to try using them as parameter packs.

template <size_t N>
auto make_tuple();  // returns tuple with N ints
template <size_t N>
int test() {
    auto [...values] = make_tuple<N>();
    return (0 | ... | values);
}

Alas, I cannot do this. Why did the committee exclude such functionality? It feels inconsistent now that lambda captures can contain parameter packs.

I know the committee is full of intelligent creative people, and an idea like mine must have come up and been rejected for good reason. What is that reason?

like image 239
Filipp Avatar asked Dec 31 '19 02:12

Filipp


1 Answers

It is the nature of a developing language that features be added over time.

Just because something is not in the language yet, does not mean that the committee already rejected it outright. It does not mean that they "excluded" it. It could just be that it was overlooked, or just hadn't been seriously considered yet. Remember, std::make_unique wasn't available until C++14.

In this particular case, it's a feature that has been proposed, quite recently, as P1061. You'll note from the revision history section that the committee "reviewed it favorably and thought this was a good investment of our time". So I guess you're in luck. :)

like image 182
Lightness Races in Orbit Avatar answered Nov 01 '22 17:11

Lightness Races in Orbit