改进基于模板的库的用例
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;
相关文章:
- 使用模板参数重载C++方法:如何使其适用于模板的子类?
- 类成员函数参数列表是否可以依赖于模板参数?
- 依赖于模板的错误
- 将 c++ 类成员函数专用于模板类
- C++ 使函数调用依赖于模板参数
- c ++如何将模板应用于模板类的孩子/朋友?
- 如何修复"没有依赖于模板参数的参数'glGenVertexArrays'......"C++ 中的错误
- 如何转发声明依赖于变量定义的类,而变体定义又依赖于模板化类?
- 动态强制转换适用于模板成员函数内的共享指针和弱指针,无需代码重复
- 如何确保 "<<" 运算符适用于模板化 ADT 定义中的任何泛型类型?
- 将 const 限定符应用于模板参数时的不同编译器行为
- 如何定义依赖于模板参数的类型定义
- 如何使用类型专用化模板方法,该类型本身就是一个模板,其中只有返回类型依赖于模板类型
- PHP 相当于C++模板
- 仅专用于模板类的一个方法(部分)
- 依赖于模板的实现
- C++ 错误:"没有依赖于模板参数的'setw'参数
- 依赖于模板参数的范围查找
- 对应于模板中的定义的声明
- 使用的类型别名如何指定依赖于模板参数的模板模板参数