In the last post i showed you how to implement a ‘Largest’ traits template using pre-C++11.
In this post I will show you how to do the same using variadic templates.
First we declare the ‘bare’ template. It tells the compiler that ‘Largest’ take an unknown number of template parameters.
Next, we create helper templates as we did before. In fact they are identical.
The main part of the template is very similar to before, but this time we just use the ellipsis as the type list.
Finally, as before we need a ‘stopper’ definition to tell the compiler when we’ve reached the end.
Putting it all together again we get…
How It’s Used
This variation has the advantage that it can handle any number of type, up to the capacity of the compiler.
You no doubt see that an additional template to handle smallest should be a trivial task.
Other additions to the templates could include the min/max size and alignment as traits.