C ++ 11-model reseller and static function proxy

Is there an easy way to do this in C ++ 11? I would like to preserve both multiple inheritance and the ability to loop through all the static functions in a package, if possible.

#include <cstdio> struct A { static void foo() {printf("fA\n");} static void bar() {printf("bA\n");} }; struct B { static void foo() {printf("fB\n");} static void bar() {printf("bB\n");} }; struct C { static void foo() {printf("fC\n");} static void bar() {printf("bC\n");} }; template <typename... T> struct Z : public T... { static void callFoos() { /* ???? WHAT THE SYNTAX T...::foo(); T::foo()...; */ } static void callBars() { /* ???? WHAT THE SYNTAX T...::bar(); T::bar()...; */ } }; int main() { Z<A, B, C>::callFoos(); Z<A, B>::callBars(); } 
+4
source share
2 answers

To expand a package, an unpacking context is needed, and building an array is one of them. Without recursion:

 static void callFoos() { int unused[] = {(T::foo(), 0)...}; (void)unused; // suppress warnings } 

The same goes for callBars .

+4
source

Add a set of manager function overloads:

 void foo_caller() { } template <typename T, typename ...Args> void foo_caller() { T::foo(); foo_caller<Args...>(); } 

Then use inside callFoos() :

 foo_caller<T...>(); 
+5
source

All Articles