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?

查看:119
本文介绍了C ++。当类T需要A型模板参数时,如何为A类定义T型模板参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Executor class has template of type P and it takes a P object in constructor. Algo class has a template E and also has a static variable of type E. Processor class has template T and a collection of Ts.

问题如何定义 Executor<处理器< Algo> > Algo< Executor> 吗?这可能吗?我看不到定义它的方法,它是一种无限递归模板参数

Question how can I define Executor< Processor<Algo> > and Algo<Executor> ? Is this possible? I see no way to defining this, its kind of an "infinite recursive template argument"

查看代码。

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;
}

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

EDIT ****************************

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

A little clarification. Executor is a singleton that provides a service. All Algo objects need the services of Executor. Executor will sometimes generate reports that need to be sent to a specific Algo object. They get sent to the correct Algo through Processor.

基本问题是需要Algo定义执行器,而Executor则需要定义Algo。

Basic issue is that Algo is needed to define Executor and Executor is needed to define Algo.

推荐答案

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

Solved! see comments. //****

#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);

}

这篇关于C ++。当类T需要A型模板参数时,如何为A类定义T型模板参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆