Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++11 typelist unroller and proxy caller of static functions

Is there a simple way to do this in C++11? I'd like to keep both the multiple inheritance AND ability to cycle thru all the static functions in the pack, 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'S THE SYNTAX 
             T...::foo();
             T::foo()...;
        */
    }

    static void callBars() {
        /* ???? WHAT'S THE SYNTAX 
             T...::bar();
             T::bar()...;
        */
    }

};

int main() {
    Z<A, B, C>::callFoos();
    Z<A, B>::callBars();
}
like image 525
user2340606 Avatar asked Jan 14 '23 12:01

user2340606


1 Answers

Add a set of dispatcher 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...>();
like image 135
Kerrek SB Avatar answered Jan 29 '23 11:01

Kerrek SB