C++11类型列表展开器和静态函数的代理调用程序

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

本文关键字:静态函数 代理 调用 程序 类型 列表 C++11      更新时间:2023-10-16

在C++11中有简单的方法吗?如果可能的话,我希望保留多重继承和循环通过包中所有静态函数的能力。

#include <cstdio>
struct A { static void foo() {printf("fAn");} static void bar() {printf("bAn");} };
struct B { static void foo() {printf("fBn");} static void bar() {printf("bBn");} };
struct C { static void foo() {printf("fCn");} static void bar() {printf("bCn");} };
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();
}

添加一组调度器函数重载:

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

然后在callFoos():内部使用

foo_caller<T...>();

Pack扩展需要一个解包上下文,数组构造就是其中之一。无递归:

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

callBars相同。