将函数指针数组中的函数指针作为模板参数传递
Passing a function pointer from an array of function pointers as a template argument
我想将函数指针数组中的函数指针作为模板参数传递。我的代码似乎使用 MSVC 进行编译,即使智能感知抱怨出了点问题。gcc 和 clang 都无法编译代码。
请考虑以下示例:
static void test() {}
using FunctionPointer = void(*)();
static constexpr FunctionPointer functions[] = { test };
template <FunctionPointer function>
static void wrapper_function()
{
function();
}
int main()
{
test(); // OK
functions[0](); // OK
wrapper_function<test>(); // OK
wrapper_function<functions[0]>(); // Error?
}
MSVC编译代码,但智能感知给出以下错误:invalid nontype template argument of type "const FunctionPointer"
GCC编译失败,并显示以下消息:
<source>: In function 'int main()':
<source>:19:33: error: no matching function for call to 'wrapper_function<functions[0]>()'
19 | wrapper_function<functions[0]>(); // Error?
| ^
<source>:8:13: note: candidate: 'template<void (* function)()> void wrapper_function()'
8 | static void wrapper_function()
| ^~~~~~~~~~~~~~~~
<source>:8:13: note: template argument deduction/substitution failed:
<source>:19:30: error: '(FunctionPointer)functions[0]' is not a valid template argument for type 'void (*)()'
19 | wrapper_function<functions[0]>(); // Error?
| ~~~~~~~~~~~^
<source>:19:30: note: it must be the address of a function with external linkage
Clang编译失败,并显示以下消息:
<source>:19:2: error: no matching function for call to 'wrapper_function'
wrapper_function<functions[0]>(); // Error?
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
<source>:8:13: note: candidate template ignored: invalid explicitly-specified argument for template parameter 'function'
static void wrapper_function()
^
1 error generated.
问题:
wrapper_function<functions[0]>();
有效与否?
如果不是,我能做些什么来functions[0]
作为模板参数传递给wrapper_function
?我的目标是在编译时构造一个新的函数指针数组,内容{ wrapper_function<functions[0]>, ..., wrapper_function<functions[std::size(functions) - 1]> }
。
由于以下原因,禁止表达式wrapper_function<functions[0]>();
:
14.3.2 模板非类型参数 [temp.arg.nontype]
非类型、非模板模板参数的模板参数 应为以下之一:
[...]
— 一个常量表达式 (5.19(,它指定 具有静态存储>持续时间和外部或内部链接的对象 或具有外部或内部链接的功能,包括功能 模板和函数模板 ID,但不包括非静态类 成员,表示(忽略括号(作为 & id-expression,除了 如果名称引用函数或数组,则可以省略 如果相应的模板参数是 参考;[...]
禁止将指针用作非类型模板参数,而不是形式&id
因此,基本上,以下内容将起作用:
static void test() {}
using FunctionPointer = void(*)();
static constexpr FunctionPointer functions[] = { test };
template <FunctionPointer function>
static void wrapper_function()
{
function();
}
int main()
{
test(); // OK
functions[0](); // OK
wrapper_function<test>(); // OK
wrapper_function<&test>(); // OK
}
使用C++14选项编译时,以下代码片段将不起作用:
constexpr auto func = &test;
wrapper_function<func>();
当使用 C++17 选项编译时,您的方法和上面的方法都将有效:
int main()
{
test(); // OK
functions[0](); // OK
wrapper_function<test>(); // OK
wrapper_function<&test>(); // OK
wrapper_function<func>(); // OK
wrapper_function<functions[0]>(); // OK
}
观看直播
- QMetaObject invokeMethod的基于函数指针的语法
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- c++r值引用应用于函数指针
- 模板函数指针和lambda
- 是否可以将llvm::FunctionType转换为C/C++原始函数指针
- 带有类的函数指针
- () 函子后面的括号,而不是函数指针?
- 全局作用域中函数指针的赋值
- 使用"Task"函数指针队列定义作业管理器
- 将成员函数指针作为参数传递给模板方法
- 如何创建对象函数指针C++映射?
- 匹配函数指针作为模板参数?
- 通过函数指针定义类范围之外的方法
- 存储在类中的函数指针
- C++从函数指针数组调用函数
- 将返回值存储在函数指针数组的指针中是如何工作的?
- 整数键映射到头文件中的成员函数指针
- 从类成员函数到类 C 函数指针的转换
- 如何将内联匿名函数分配给C++函数指针
- 将字符缓冲区强制转换为函数指针