【问题标题】:Recursive template?递归模板?
【发布时间】: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


    【解决方案1】:

    您不能将递归模板命名为 outside,但可以将其命名为 inside,因为参数列表在自引用中是可选的。

    然后问题就变成了告诉模板如何将自己传递给“某物”。

    template< typename T >
    struct fptr_taking_type {      // this template is essentially a function
        typedef void (*type)( T ); // from types to types, the result being
    };                             // the typedef
    
    template< typename T >
    struct stdfn_taking_type {
        typedef function< void (*)( T ) > type;
    };
    
    template< template< typename > class F >
    struct G {
        typename F< G * >::type m_func; // this declares the member variable
    };
    
    ...
    
    G< fptr_taking_type > q;
    

    【讨论】:

    • 谢谢。我会试试这个。 :)
    【解决方案2】:

    C 是类模板,而不是类。您不能拥有C 类型的对象或指向C 的指针;你只能拥有C 的实例化对象,例如C&lt;int&gt;C&lt;float&gt;

    【讨论】:

      【解决方案3】:

      在这一行:

      CC *)> c;

      粗体 C(添加了重点)没有指定模板参数。您应该通过在 括号中指定类型来指定此函数指针采用的 C* 类型。

      【讨论】:

      • 谢谢。我理解编译器为什么抱怨。但我不知道如何解决这个问题。 C*)>* )> 显然不起作用。 :(
      • 他们说的是模板 class C { public: C() {} Func m_func; }; void foo() { C)(C)> c; C<:function>*)>> d; } int main(){ }
      • 是的,我明白了。但我希望 C* 成为它自己类型的指针。 IE。如果整个东西不是模板,它看起来像这样: class C { void( * m_func)(C * ); };
      • 或者这个:class C { std::function m_func; };
      【解决方案4】:

      这有帮助吗?

      void foo1(){}
      
      template<typename Func> 
      class C 
      { 
      public: 
          C(Func f) : m_func(f) {} 
          Func m_func;
          C<Func> *mp;                 // it is a pointer,
      }; 
      
      void foo() 
      { 
          C<void (*)(void)> c (foo);
      } 
      
      int main(){
          C<void (*)(void)> c(foo);
      }
      

      【讨论】:

        【解决方案5】:

        你不能有一个递归模板。

        【讨论】:

        • 有没有模板不支持的递归?
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-20
        • 2012-01-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多