我可以使用模板功能参数C 更改模板类型
Can i change template class type using template function parameter c++
我想将类的类型从t更改为new_t,是否可能?
template<class T,int count>
class obj_pool{
public:
template<class new_T>
void Retypedef();
};
例如,为int obj_pool<int> pool
创建的obj_pool,当用户调用 pool.Retypedef<double>()
函数时,该池成为double的池。
no。
将来,当您写一个问题时,包括有关您要解决的基本问题的一些信息。然后,当您的意识解决方案是一个死胡同时,回答的人可以解决您的根本问题。
或询问有关该基本问题的新问题(通过问问题按钮)。
在您的示例中, T
和 new_T
是两个不同的参数。使用此模板可能看起来像:
obj_pool<int,3> x;
x.Retypedef<double>(); // <- passing different types for new_T
x.Retypedef<float>(); // <- does not change the type of x
x.Retypedef<int>(); // ..and of course you can use the same type for
// new_T as for T
如果您希望new_T
总是与T
相同,则可以简单地写:
template<class T,int count>
class obj_pool{
public:
void Retypedef(); // can use T
};
相关文章:
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- C++从成员类型中扣除类型的功能模板?
- 这种错误的原因是什么:将"功能"重新定义为不同类型的符号
- 模板功能:根据类型名称进行转换
- 如何在同一个模板功能上专门化几种类型?
- 错误:未知类型名称"功能跟踪"
- 我对C 中共享指针列表进行排序的功能未完成类型
- 共享_ptr中多功能数组的类型是什么
- 确定功能主体中的返回类型
- 返回类型不可知模板类成员功能
- 类型推论模板功能返回类型
- C 功能模板指定第二个模板参数类型
- 为什么指定数据类型而不是构造功能参数?C
- C 尝试使用模板输出功能来显示不同类型的数据
- 为什么BOOST :: HANA :: EXAREMIS ::键入实验功能?类型列表的棘手是什么
- 将多种类型传递给STD ::功能C
- C 中此功能的返回类型的含义
- C++ 使用包含不同类型功能对象的基和派生类的 Reactor
- 请求从非标量类型转换为非标量类型(功能)