C++11类型列表展开器和静态函数的代理调用程序
C++11 typelist unroller and proxy caller of static functions
在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
相同。
相关文章:
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 检查编译时是否存在静态函数
- 名称隐藏对静态函数继承的实例使用
- 如果 C 函数仍然可以间接执行(通过回调函数),那么将它声明为静态函数是否是一种不好的做法?
- 类中静态函数C++意外结果
- 在工人类中使用不同类的静态函数进行实验
- 类 Referention 中C++回调函数引用非静态函数
- 指向模板上下文中的成员函数或静态函数的指针
- 如何检测 Clang AST C++中的静态函数
- 内联asm编译器屏障(内存阻塞器)是算作外部函数,还是算作静态函数调用
- 如何在静态函数中使用成员函数数组
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 生成代码(在编译时)以调用模板的每个实例化的静态函数
- C++无法访问或使用静态函数
- 如何将 cpp 文件中的静态函数公开给其他文件
- 将函数的引用设置为其他 c++ 文件中的非静态函数
- 方法的静态函数的等价性
- 扩展包含静态函数的类
- 非静态函数可以访问静态变量吗?
- C++11类型列表展开器和静态函数的代理调用程序