小编Mat*_*tti的帖子

将函数指针数组中的函数指针作为模板参数传递

我想将函数指针数组中的函数指针作为模板参数传递。我的代码似乎使用 MSVC 编译,即使 Intellisense 抱怨出现问题。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?
}
Run Code Online (Sandbox Code Playgroud)

MSVC编译代码,但 Intellisense 给出以下错误: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 | …
Run Code Online (Sandbox Code Playgroud)

c++ templates c++14

10
推荐指数
1
解决办法
214
查看次数

标签 统计

c++ ×1

c++14 ×1

templates ×1