Variadic Templating

This is more of a concept question:

I've learned enough C++ that using typical stuff that people do in the their first year of programming doesn't satiate my desire for complex-enough projects.

I've done all that: string manipulation, recursion, data structures, templating, etc. I've even done enough multi-threading in operating systems. I even helped write a simple-ish multi-core CPU scheduler.

I've looked into variadic templates. My main question is:
What is the practical use of this? I can see that it allows you to take a variable number of arguments, but what practical application does this have? MSDN gives an example of them to implement "print" with any number of arguments.
I'm interested to hear how I might implement these into my own projects. I'm having trouble thinking of uses.
A very common use case is implementing polymorphic callbacks. Two simple examples:
http://www.cplusplus.com/forum/general/120687/
http://www.cplusplus.com/forum/general/120728/
A few examples from the standard library itself:

The std::lock function template, which takes arbitrary many mutexes and locks them all, while avoiding a deadlock: http://en.cppreference.com/w/cpp/thread/lock

std::make_shared, std::allocator::construct, std::vector::emplace, and many other function templates that forward their arbitrary many arguments into the parameters of some constructor, in order to construct an object in-place: http://en.cppreference.com/w/cpp/memory/shared_ptr/make_shared
http://en.cppreference.com/w/cpp/memory/allocator/construct
http://en.cppreference.com/w/cpp/container/vector/emplace

in a similar vein, std::function, std::bind, std::packaged_task, std::async, and other types that forward arbitrary many arguments into the parameters of some function, possibly invoked at a later time (a callback, as JLBorges already pointed out)
http://en.cppreference.com/w/cpp/utility/functional/function
http://en.cppreference.com/w/cpp/utility/functional/bind
http://en.cppreference.com/w/cpp/thread/async
http://en.cppreference.com/w/cpp/thread/packaged_task

std::common_type, std::make_tuple, std::conjunction, std::is_constructible, std::result_of, and other metafunctions that take arbitrary many types and perform some compile-time manipulations on them
http://en.cppreference.com/w/cpp/types/common_type
http://en.cppreference.com/w/cpp/utility/tuple/make_tuple
http://en.cppreference.com/w/cpp/types/conjunction
http://en.cppreference.com/w/cpp/types/is_constructible
http://en.cppreference.com/w/cpp/types/result_of

in a similar vein, std::integer_sequence which is a metafunction over arbitrary many non-types, is also variadic: http://en.cppreference.com/w/cpp/utility/integer_sequence


Some great examples! Thanks!

I like the connection to the standard library - so I've been using variadic templates without knowing it.

You may see some question from me on the forums as I begin exploring these and incorporating them into my projects.

The polymorphic callback idea is awesome - I was always curious about how that was done.
Topic archived. No new replies allowed.