【发布时间】:2016-11-24 07:32:33
【问题描述】:
我有一个模板类:
template<typename A, typename B, typename C>
class blah{...};
我想创建一个 std::vector 来保存任何可能的 blah 对象。
例如
blah<float, int, string>
或
blah<string, string, char>
这些 blah 对象虽然来自同一个模板类,但本质上是不同的类型。如何声明一个 std::vector 来保存它们?
我想过使用std::variant,但我不能,因为要使用它,我需要枚举所有可能的模板参数组合:
std::variant<all possible template parameter combinations>
我考虑过使用 std::any,但我不能,因为我不知道如何从 std::any 类型转换回正确的类型?
我考虑过使用基类:
template<typename A, typename B, typename C>
class blah : public blah_base
{...};
并像这样声明一个向量:
std::vector<blah_base*>
但是,我仍然不知道如何从 blah_base* 类型转换回正确的类型?
【问题讨论】:
-
您如何使用
blah<...>实例?你的用例是什么?不能用继承和虚函数解决吗?您尝试解决的实际问题是什么? -
你能提供
bla的定义还是太长了? -
我正在尝试实现深度学习神经元网络。 blah 类型是张量。张量可以有不同的维度、设备类型和数据类型。所以真正的 blah 被定义为“template
class {...};” -
老实说,我认为我的用例并不重要。我只想用一个虚拟类来简化我的问题。但如果你真的想知道代码,你可以在这里查看:github.com/dmlc/mshadow/tree/master/guide#tensor-data-structure
标签: c++ templates types metaprogramming template-meta-programming