c++ - 将函数作为模板参数传递

标签 c++ templates gcc g++ template-meta-programming

<分区>

我正在移植我写到 GCC 的一些 MSVC 代码,但它无法在 GCC 上编译(参见:https://ideone.com/UMzOuE)。

template <const int N>
struct UnrolledOp
{
    template <const int j, int op(int*, int*)>
    static void Do(int* foo, int* l, int* r)
    {
        return UnrolledOp<N - 1>::Do<j + 4, op>(foo, l, r);
    }
};

template <>
struct UnrolledOp<0>
{
    template <const int j, int op(int*, int*)>
    static void Do(int* foo, int* l, int* r) { }
};

template <const int fooSize, int op(int*, int*)>
void Op(int* foo, int* l, int* r)
{
    UnrolledOp<fooSize / 4>::Do<0, op>(foo, l, r);
}

int Test(int* x, int* y)
{
    return 0;
}

int main()
{
    Op<16, Test>(nullptr, nullptr, nullptr);
    return 0;
}

出于某种原因,GCC 不喜欢我将 op 传递给其他模板函数的方式。

最佳答案

Do需要使用template关键字,这是一个函数模板。例如

UnrolledOp<fooSize / 4>::template Do<0, op>(foo, l, r);
//                       ~~~~~~~~

LIVE

关于c++ - 将函数作为模板参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49172974/

相关文章:

c++ - 类模板的友元函数的显式特化

c - 我是否误解了 GCC 中的 __attribute__ ((packed)) ?

c++ - 从继承类 C++ 获取模板类型

c++ - 经典的 C++ 静态初始化顺序惨败重温

c - C (GCC) 宏扩展期间的语句表达式语法错误

c++ - 在引用 : Linking C compiled static library to C++ Program

c++ - 在 OOP 中传递堆栈对象

c++ - Gettext 和语言环境

c++ - 我如何在 opencv 中声明一个 3 维矩阵

C++ DLL 不加载调试符号