【发布时间】:2010-09-07 04:48:19
【问题描述】:
改写问题
我发现我最初的问题不够清楚,回复者误解了我的问题。所以让我试着澄清一下:
假设我有两个班级:
struct C { void(*m_func)(C*); };
struct D { std::function<void(D*)> m_func; };
现在我想制作两者的通用版本,所以我做了这样的事情:
template<typename Func>
struct G
{
Func m_func;
};
但是现在我不知道如何实例化这个类:
G<void(*)(G*)> c; //error
G<std::function<void(G*)>> d; //error
G<void(*)( G<void(*)(G<???>*)> *)> c; //???
G<std::function<void( G<std::function<void(G<???>*)>> *)>> d; //???
原问题:
嗨,
我有一个模板类,它可以将函数指针或 std::function 对象作为其参数。一切都很好,直到该函数在其签名中使用模板类的指针:
#include <functional>
template<typename Func>
class C
{
public:
C() {}
Func m_func;
};
void foo()
{
C<void(*)(C*)> c;
C<std::function<int(C*)>> d;
}
相关编译错误:
error C2955: 'C' : use of class template requires template argument list
error C3203: 'function' : unspecialized class template can't be used as a template argument for template parameter 'Func', expected a real type
error C2955: 'std::tr1::function' : use of class template requires template argument list
它是如何解决这个问题的?
【问题讨论】:
标签: c++ templates function stl