c++ - 从类对象访问模板参数
问题描述
我在 myclass.hpp 中有一个类模板:
template<class T, class P>
class myclass
{
....
};
在我的 main.cc 中,我创建了一个类的对象:
myclass<int, double> mc;
otherfunc<myclass>(mc);
在其他一些头文件 header1.hpp 中:
template<class MyClass>
void otherfunc(MyClass const &mc)
{
/* Access through 'mc' the underlying template parameters T and P*/
}
如何访问 header1.hpp 中的模板参数 T 和 P?
解决方案
如何访问 header1.hpp 中的模板参数 T 和 P?
在您的类中提供public
类型定义myclass
:
template<class T, class P>
class myclass
{
public:
typedef T T_type;
typedef P P_type;
....
};
因此,您可以访问这些类型
typename myclass::T_Type x;
typename myclass::P_Type y;
别处。
推荐阅读
- opentap-proj - 检测移除的TestStep
- javascript - 在纯 JavaScript 中访问属性值时出错
- c - C中嵌套深度问题中的运行时错误
- java - 如何使用改造(Android、Java)发布多个 JSON 数组
- ios - 如何在 Swift 中创建弹跳动画
- c++ - wcstof 精度
- orc - Azure SQL 数据仓库 (Synapse Analytics) 使用 ORC 表的 Polybase 性能
- spring - Spring BCryptPasswordEncoder - 任何方式来配置散列生成的轮数?
- android - Android:更改复选框颜色(框背景和刻度线颜色)
- react-native - 反应原生元素 - 为 listItem 中的徽章背景颜色应用条件样式