5

Is it theoretically possible to add a language feature to unpack structures into the function actual parameter lists? I mean the following.

template< typename ...Ts >
void f(Ts... values) { (std::cout << ... << values) << std::endl; }

struct S { int a; char c; double d; };
S s{1, '2', 3.0};
f([s]);

void g(int, int, int) {}

g([s]); // warning about narrowing conversion

void h(int &, int &, int &) {}

h([s]); // hard error ("cannot bind to...")

It would be handy to deal with suitable structures of unknown count of members. Because current structured bindings can't "unpack" structures of unknown number of components (like auto... [x] = s;) due to lack of the template context, where only possible operator ... to deal with variadic number of types/values.

What downsides can be on this way?

Tomilov Anatoliy
  • 15,657
  • 10
  • 64
  • 169
  • 1
    `f` aside, you could use https://github.com/apolukhin/magic_get, turn it into a `std::tuple`, and use `std::apply`. – chris Feb 18 '17 at 08:18
  • @chris I am familiar with the technique, but there is too much boilerplate code to deal with reasonable numbers of components in generic code if I want e.g. to [get a n-th `struct` field](https://gist.github.com/tomilov/0cf10f110a8e1a0b486603bba500640a). I am awared about workarounds. – Tomilov Anatoliy Feb 18 '17 at 08:21
  • 1
    The reason there's hacky boilerplate is that there's no directly supported way to do this. The technique piggybacks on a feature of the language designed for a different problem. But the boilerplate shouldn't be a big deal if using a library instead of copying/adapting code into your project. And honestly, I would find a structure with more members than is supported (appears to be 100) to be pretty insane. – chris Feb 18 '17 at 08:23
  • 3
    Side note - this feature isn't actually called *structured binding* but *decomposition declaration*. see e.g. [here](http://stackoverflow.com/a/41570596/4324224) – W.F. Feb 18 '17 at 10:00
  • @Orient: You think too small. [This](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2017/p0535r0.html) is a far better idea. – Nicol Bolas Feb 18 '17 at 15:22
  • @NicolBolas Sure =). It is just a first thought. But huge syntactic innovations are hardly possible to be accepted by the Committee. – Tomilov Anatoliy Feb 18 '17 at 15:49
  • @Orient: "*But huge syntactic innovations are hardly possible to be accepted by the Committee.*" Nothing gets accepted by the committee from a Stack Overflow question. The committee only responds to *papers* that are submitted and presented, which that one is. So despite being a "huge syntactic innovation", it's far more likely that the paper gets accepted than your post here. – Nicol Bolas Feb 18 '17 at 16:00
  • @NicolBolas It is obvious. My question is not a proposal. – Tomilov Anatoliy Feb 18 '17 at 17:12
  • @Orient: Your question is also *pointless*. Could you add such a language feature? Sure; in the sense that you could write a proposal, present it to the committee, and get them to agree with it. So I don't see what the point of asking about it on SO is. – Nicol Bolas Feb 18 '17 at 17:14
  • @NicolBolas can you help me to find a proper place to ask such a questions? – Tomilov Anatoliy Feb 18 '17 at 17:17
  • 1
    @Orient: I could suggest the [ISO C++ Future Proposals forum](https://groups.google.com/a/isocpp.org/forum/#!forum/std-proposals), but the paper I linked to was heavily discussed there, and yours would be deemed a pale imitation. Which... it is. Also, if you're going to bring it up on that form anyway, I would advise you to put more thought into it than you've shown in this question. You should have some idea as to what `[s]` means exactly. You should know how `[s]` behaves, whether or not other arguments are allowed along side it. And so forth. – Nicol Bolas Feb 18 '17 at 17:22
  • @NicolBolas Don't worry about my inner world =). I can handle. But current proposal (atm implemented in *clang++* and *g++*) is even more poor in a way then linked proposal. I see contradiction in what you said with real world. – Tomilov Anatoliy Feb 18 '17 at 21:50
  • @Orient: Um... what "current proposal" are you talking about? The one I linked to isn't available in any compiler. And the one you're talking about doesn't exist. At least, not that I'm aware of. – Nicol Bolas Feb 18 '17 at 21:55
  • @NicolBolas I am talking about [*structured bindings*](http://wg21.link/p0217r3) of course. – Tomilov Anatoliy Feb 19 '17 at 06:54

0 Answers0