use*_*606 4 c++ templates c++11
在C++ 11中有一个简单的方法吗?如果可能的话,我想保持多重继承和能力循环通过包中的所有静态函数.
#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();
}
Run Code Online (Sandbox Code Playgroud)
添加一组调度程序函数重载:
void foo_caller() { }
template <typename T, typename ...Args>
void foo_caller()
{
T::foo();
foo_caller<Args...>();
}
Run Code Online (Sandbox Code Playgroud)
然后在里面使用callFoos():
foo_caller<T...>();
Run Code Online (Sandbox Code Playgroud)