代码之家  ›  专栏  ›  技术社区  ›  Zach Saw

将函数作为模板参数传递[重复]

  •  1
  • Zach Saw  · 技术社区  · 7 年前

    我正在移植一些MSVC代码,这些代码是我编写到GCC的,但在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 到其他模板函数。

    1 回复  |  直到 7 年前
        1
  •  4
  •   songyuanyao    7 年前

    您需要使用 template 的关键字 Do ,这是一个函数模板。e、 g。

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

    LIVE