views:

119

answers:

2

Is there a macro that tells me whether or not my compiler supports variadic templates?

#ifdef VARIADIC_TEMPLATES_AVAILABLE

template<typename... Args> void coolstuff(Args&&... args);

#else

???

#endif

If they are not supported, I guess I would simulate them with a bunch of overloads. Any better ideas? Maybe there are preprocessor libraries that can ease the job?

+8  A: 

Maybe: #ifndef BOOST_NO_VARIADIC_TEMPLATES?


If variadic templates are not supported, you might think of using the boost tuple library:

template<typename Tuple> void coolstuff(Tuple&& args);

And:

coolstuff(boost::make_tuple(1, 2, 3));
Polybos
+6  A: 

It looks like the current version of Boost defines BOOST_NO_VARIADIC_TEMPLATES if variadic templates are unavailable. This is provided by boost/config.hpp; see here for config.hpp documentation.

If variadic templates are unavailable, then you'll probably have to simulate them with a bunch of overloads, as you said. The Boost.Preprocessor library can help here; it's designed to automate all sorts of repetitive source code, including template overloads. You can search the Boost source trees for BOOST_NO_VARIADIC_TEMPLATES for examples on using it to simulate variadic templates.

Josh Kelley
I would combine your approach with Niels': propose an interface with many overloads of various arities thanks to Boost.Preprocessor which wraps the parameters into a `Boost.Fusion` container (tuple, vector, etc...) that is then passed to a single template class expecting it in which the actual implementation is done. The idea is simply to reduce the amount of code in the macros to ease debugging :)
Matthieu M.