fredoverflow
fredoverflow

Reputation: 263148

How do I compile variadic templates conditionally?

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?

Upvotes: 7

Views: 578

Answers (2)

Karl von Moor
Karl von Moor

Reputation: 8614

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));

Upvotes: 8

Josh Kelley
Josh Kelley

Reputation: 58372

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.

Upvotes: 7

Related Questions