Recursive pattern?

Rephrasable Question

I found that my initial question was not clear enough, and the defendants misunderstood my problem. So let me clarify:

Let's say I have two classes:

struct C { void(*m_func)(C*); };
struct D { std::function<void(D*)> m_func; };

Now I want to create a generic version of the two, so I am doing something like this:

template<typename Func>
struct G
{
Func m_func;
};

But now I do not know how to instantiate this class:

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;  //???

The original question:

Hi,

I have a template class that can take a function pointer or a std :: function object as a parameter. Everything is fine until this function uses the template class pointer in its signature:

#include <functional>

template<typename Func>
class C
{
public:
    C() {}
    Func m_func;
};

void foo()
{
    C<void(*)(C*)> c;
    C<std::function<int(C*)>> d;
}

Relevant compiler errors:

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

How to solve this problem?

+3
source share
5

, , .

, "-".

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<int> C<float>.

+2

:

   C<void(*)(C *)> c;

C ( ) . , C* , < gt; .

+2

?

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);
}
+1
source

You cannot have a recursive template.

0
source

Source: https://habr.com/ru/post/1763549/


All Articles