c++ - How to extract __VA_ARGS__? -
i hava macro call static function each args.
for example:
#define foo(x) x::do(); #define foo_1(x,y) x::do(); y::do();
my question need use foo variable number of arguments, possible use __va_args__
?
like line below:
#define foo(...) __va_args__::do() ?
thanks
macro expansion not work argument pack expansion variadic templates. have expand to:
x,y::do();
and not to
x::do(); y::do();
as hoped. in c++11 use variadic templates. instance, want way:
#include <iostream> struct x { static void foo() { std::cout << "x::foo()" << std::endl; }; }; struct y { static void foo() { std::cout << "y::foo()" << std::endl; }; }; struct z { static void foo() { std::cout << "z::foo()" << std::endl; }; }; int main() { do_foo<x, y, z>(); }
all need (relatively simple) machinery:
namespace detail { template<typename... ts> struct do_foo; template<typename t, typename... ts> struct do_foo<t, ts...> { static void call() { t::foo(); do_foo<ts...>::call(); } }; template<typename t> struct do_foo<t> { static void call() { t::foo(); } }; } template<typename... ts> void do_foo() { detail::do_foo<ts...>::call(); }
here live example.
Comments
Post a Comment