小编Fed*_*aus的帖子

为什么不能将模板函数作为模板模板参数传递?

当我尝试将函数作为模板模板类型参数传递给类时,遇到错误。为什么语言核心开发人员将无法启用此功能?可以传递函子类模板,但不能传递函数模板。

例如,在“ g ++(Ubuntu 8.3.0-6ubuntu1)8.3.0”中编译以下代码:

#include <iostream>

using namespace std;

template <template <typename> typename Functor>
class UseFunc
{
public:
    void use()
    {
        Functor<int>(3);
        Functor<char>('s');
    }
};

template <typename T>
void func(T s)
{
    cout << s << endl;
}

int main()
{
    UseFunc<func> u {};
    u.use();
}
Run Code Online (Sandbox Code Playgroud)

告诉:

kek.cpp: In function ‘int main()’:                                                                                                           
kek.cpp:24:14: error: type/value mismatch at argument 1 in template parameter list for ‘template<template<class> class Functor> class UseFunc’
  UseFunc<func> u {};                                                                                                                        
              ^                                                                                                                              
kek.cpp:24:14: note:   expected a class template, …
Run Code Online (Sandbox Code Playgroud)

c++ templates function functor

9
推荐指数
1
解决办法
78
查看次数

标签 统计

c++ ×1

function ×1

functor ×1

templates ×1