C .当T类需要A类模板参数时,如何定义A类T型的模板参数

C++. How to define template parameter of type T for class A when class T needs a type A template parameter?

本文关键字:参数 定义 何定义      更新时间:2023-10-16

executor类具有P型模板,并且在构造函数中采用P对象。Algo类具有模板E,并且具有E类型的静态变量。处理器类具有模板T和TS的集合。

问题如何定义Executor< Processor<Algo> >Algo<Executor>?这可能吗?我认为无法定义这一点,这是一种"无限递归模板参数"

请参阅代码。

template <class T>
class Processor { 
    map<string,T> ts;
    void Process(string str, int i)
    {
        ts[str].Do(i);
    }
} 
template <class P>
class Executor {
    P &p;
    Executor(P &inp) : p(inp) {}
    void Bar(string str, int i) {
        p.Process(str,i);
    }
    Execute(string str)
    {
    }
} 
template <class E>
class Algo
{
    static E e;
    void Do(int i) {}
    void Foo()
    {
        e.Execute("xxx");
    }
}
main ()
{
    typedef Processor<Algo> PALGO; // invalid
    typedef Executor<PALGO> EPALGO;
    typedef Algo<EPALGO> AEPALGO;
    Executor<PALGO> executor(PALGO());
    AEPALGO::E = executor;
}

编辑* ** * ** ** ** ** ** * ** * ** * ** * ** ** *** *** *** ***

一点澄清。执行人是提供服务的单身人士。所有算法对象都需要执行器的服务。执行人有时会生成需要发送到特定算法对象的报告。他们通过处理器将其发送到正确的算法。

基本问题是定义执行程序需要算法,并且需要执行器来定义算法。

尝试重现代码,而不太确定您要实现的目标。对于初学者来说,这就是我对其进行了修改:

   #include <string>   //Added
   #include <map>      //Added
   using namespace std;//Added
   template <class T>
   class Processor {    
      map<string,T> ts;    
      void Process(string str, int i) {        
         ts[str].Do(i);    
      }
   };
   template <class P>
   class Executor {
      Processor<P> &p;    //Was Proc ???
      Executor(P &p) : Processor<P>(p) {}    //Was Proc ???
      void Foo(string str, int i) {
         p.Process(str,i);
      }
      void Execute(string str){}  //Added return type void
   };
   template <class E>
   class Algo {
   public:                 //Added
      static E e;
      void Do(int i) {}
   };
   main () {
      typedef Processor< Algo<int> > PALGO; //Added template argument to Algo
      typedef Executor<PALGO> EPALGO;
      typedef Algo<EPALGO> AEPALGO;
      Executor<PALGO> executor(PALGO());
      AEPALGO::e = executor;
   }

在执行程序定义中修改了Proc到处理器 - (什么是proc?),并在Typedef处理器> PALGO中给出了模板参数;然后aepago :: e->那就是模板参数,而不是类Algo成员 - So Aepago :: e。现在,您将获得一个更易于管理的错误。它需要一个复制构造函数来转换类型。

您可以使用继承。

class X : public Executor<Processor<Algo<X>>> {};

其他,这是不可能的。

afaics,您不能使用 same Executor type做到这一点。否则您将不得不定义

Executor<Processor<Algo<Executor<Processor<Algo<...> > > > > >

如果您用其他类型定义它,它可能会起作用,只要技术上有任何有意义的技术

class X {
...
};
Executor<Processor<Algo<Executor<Processor<Algo<X> > > > > >

或使用typedef

class X {...};
typedef Processor<Algo<X> > PALGO;
typedef Executor<PALGO> EPALGO;
typedef Algo<EPALGO> AEPALGO;
Executor<PALGO> executor(PALGO());

由于执行程序是单身人士,因此您可以将其定义移出其自己的Singleton类或executor中的算法。然后使所有需要了解执行器模板成员函数的算法函数。

template <class P>
class Executor {
    static Executor e;
    P &p;
    Executor(P &p) : Proc(p) {}
    void Bar(string str, int i) {
        p.Process(str,i);
    }
    Execute(string str)
    {
    }
    public:
    static Executor& getE(){ return e;}
} 
class Algo
{
    void Do(int i) {}
    template <class E>
    void Foo()
    {
        E::getE().Execute("xxx");
    }
}

解决!看评论。//* ***

#include <string>
#include <map>
#include <iostream>
using namespace std;
template <class T>
class Processor {
public:
    map<string,T*> ts;
    void Process(string str, int i)
    {
        ts[str]->Do(i);
    }
};
template <class P>
class Executor  {
public:
    P &p;
    Executor(P &inp) : p(inp) {}
    void Bar(string str, int i) {
        p.Process(str,i);
    }
    void Execute(string str)
    {
        cout << " Executor::Execute " << str << endl;
    }
};
template <template <class> class E> //**********
class Algo
{
    string str;
public:
    Algo(const string &s) : str(s) {}
    static E<Processor<Algo>> *e; //**********
    void Do(int i) { cout << str << "::Do(" << i <<")"<< endl; }
    void Foo()
    {
        e->Execute(str);
    }
};
template <template <class> class E>
E< Processor<Algo<E> > >* Algo<E>::e;  //**********
int main(int argc, char **argv)
{
    typedef Algo<Executor> EALGO;
    typedef Processor<EALGO> PALGO;
    typedef Executor<PALGO> EPALGO;
    PALGO p;
    EPALGO executor(p);
    EALGO::e = &executor; //**********
    EALGO ealgo1("algo1"), ealgo2("algo2");
    p.ts["algo1"] = &ealgo1;
    p.ts["algo2"] = &ealgo2;
    ealgo1.Foo();
    ealgo2.Foo();
    executor.Bar("algo1",1111);
    executor.Bar("algo2",2222);
}