改进基于模板的库的用例

Improve the usecase of a template-based library

本文关键字:于模板      更新时间:2023-10-16

我正在使用模板在 c++ 中创建一个库。

我试图解决的问题,本身的用例很烦人。

首先创建一个带有模板的容器"Q_fun",其余类使用它,因此所有类都具有相同的模板参数。

主.cpp

lib::Discrete<unsigned> status(3),actions(21);
lib::Q_fun<double,1,1> q(status, actions);
lib::Agent<double,1,1> a(q);
lib::Trainer_TD<double,1,1> tr(a);

自由:

template<typename T, unsigned st, unsigned ac>
class Agent
{
public:
Q_fun<T,st,ac> *q;
...
}
... other file ...
template<typename T, unsigned st, unsigned ac>
class Trainer_TD
{
public:
Agent<T,st,ac> *a;
...
}

你知道更好的方法来改善这一点吗(我可以更改什么库(?

谢谢4你的时间。 帕克

使用类型特征:

template <typename T,unsigned st, unsigned ac>
struct my_types {
using Agent_t = Agent<T,st,ac>;
using Q_fun_t = Q_fun<T,st,ac>;
using Trainer_TD_t = Trainer_TD<T,st,ac>;
};

现在不需要重复模板参数:

using types = my_types<double,1,1>;
types::Agent_t agent;
types::Q_fun_t q_fun;
types::Trainer_TD_t trainer_qd;