【问题标题】:Passing function as template parameter [duplicate]将函数作为模板参数传递[重复]
【发布时间】:2018-03-08 12:20:49
【问题描述】:

我正在移植一些我写到 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 传递给其他模板函数的方式。

【问题讨论】:

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


    【解决方案1】:

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

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

    LIVE

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      相关资源
      最近更新 更多