Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You should see the code that variadic templates replace.


Say more! I'd like to know what you mean.


Before variadic templates you couldn't define a function template taking an arbitrary number of arguments; what you could do is define N overloads up to a finite large N. This quickly becomes tedious and hard to maintain, so you either write an external generator script, or 'creatively' use the preprocessor. These solutions were also not much easier to maintain and of course would kill compilation times.

The worst part is that often these were forwarding functions, that is, they didn't do anything directly with their parameters, but simply forwarded them to some other function (this happen surprisingly often in highly generic code). To do forwarding correctly, you have to handle const and non const reference arguments properly, which means that N overloads are not enough and you need N*2^N (someone please check my math) overloads. As you can imagine, this was impractical for N > 3.

Variadic templates plus perfect forwarding via magic references make easy, practical and possible what was impractical before.

edit: spelling




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: