将未定义的参数计数作为模板参数的函数指针
Function pointer with undefined parameter count as template argument
我正在寻找一种方法来传递具有未定义参数计数和类型作为模板参数的函数指针。
在我的研究中,我已经发现了这个
STC<void (*)()> s(foo2, fp); // like this
因此,通常似乎可以将函数指针作为模板参数传递。我现在的问题是是否有可能做这样的事情
STC<void (*)(T&&... t)> s(foo2, fp);
附加信息:我想传递函数指针的类应该只保存数组中的函数列表,没有其他函数。
如果我理解正确,您正在寻找部分专业化。
我的意思是类似的东西(感谢 Jarod42 的改进(
template <typename>
struct foo;
template <typename ... Ts>
struct foo<void(*)(Ts ...)>
{ void(*ptr)(Ts ...); };
但请注意,模板参数不是函数指针;而是函数指针的类型。
以下是完整的工作示例
#include <iostream>
template <typename>
struct foo;
template <typename ... Ts>
struct foo<void(*)(Ts ...)>
{ void(*ptr)(Ts ...); };
void func1 ()
{ std::cout << "func1" << std::endl; }
void func2 (int, long)
{ std::cout << "func2" << std::endl; }
int main ()
{
foo<decltype(&func1)> f1 { &func1 };
foo<decltype(&func2)> f2 { &func2 };
f1.ptr();
f2.ptr(1, 2L);
}
相关文章:
- 将可变参数函数的参数封装在类实例中
- QML 使用带有参数C++函数
- 使用可变参数函数作为模板参数
- 如何在C++中伪造虚拟可变参数函数模板?
- 为什么可变参数函数不适用于模板
- C++ std::functional 中的可变参数函数模板
- 可变参数函数指针的定义对于VxWorks spyLib来说不清楚
- 使用可变参数函数覆盖具有不同函数签名的虚函数
- 考虑引用和常量的可变参数函数包装器
- 使用可变参数函数将整数和/或整数数组放入单个 int 数组中
- 在可变参数函数中转发特定范围的参数
- 通过引用传递参数;函数返回类型是否必须为 VOID?
- 使用带有一个参数函数的递归找到数字的平方
- 可变参数函数模板不能很好地使用 std::function 作为参数
- 多个可变参数函数的单个模板参数包?
- 参数数据类型未知的可变参数函数
- 可变参数函数参数包扩展
- 使用模板可变参数函数将多个参数传递给另一个函数
- 对可变参数函数的递归调用的链接器错误
- 通过像printf这样的可变参数函数传递一个带有常量字符*转换函数的类