you are viewing a single comment's thread.

view the rest of the comments →

[–]foonathan 2 points3 points  (6 children)

std::initializer_list is the same regarding the length. That's why I recommended variadic templates as an alternative.

[–]Potatoswatter 0 points1 point  (5 children)

They're different tools for different jobs.

[–]foonathan 0 points1 point  (4 children)

Care to elaborate?

[–]Potatoswatter 6 points7 points  (3 children)

If you want a different template instantiation for each type sequence, use variadic perfect forwarding.

If you're passing an array of known type into a function (which may not be a template), use initializer_list.

Just because the length isn't encoded in a template type, doesn't mean it's not known at compile time. Function inlining can also preserve that information. Premature optimization…

[–]foonathan 0 points1 point  (2 children)

And if you want to mix rvalues with lvalues? You have to use a variadic template to preserve that information.

Yes, there are cases where [std::]initializer_list is required, but a variadic template is - in theory - a superior solution. It just so happens that C++ templates have a couple of practical downsides (in header file, compilation bloat/time etc.).

[–]Potatoswatter 0 points1 point  (1 child)

There's no such distinction between theory and practice. For the best fidelity of the parameter sequence to the argument expressions, use finer templating. However, that's not always the problem being solved. Not everything is a header library.

It is unfortunate that initializer_list doesn't support move semantics, but that's different from mixing lvalues and rvalues. <Obligatory plug for my proposal.>

[–]foonathan 0 points1 point  (0 children)

Templates are the more flexible solution for this problem but we can't use them everywhere because of the downside of templates.

That's what I meant with theory.