【发布时间】:2020-10-25 16:48:07
【问题描述】:
我不是模板元编程方面的专家,我现在有点卡住了。任何帮助将不胜感激。
作为介绍。 我有一堂课(这里稍微简化了一点):
template < int dim, int spacedim, int... structdims >
class TopologyInfo
{
}
我可以使用函数创建TopologyInfo 的实例:
template < int dim, int spacedim, size_t... dims >
auto get_topology_info_imp_( std::integer_sequence< size_t, dims... > )
{
return TopologyInfo< dim, spacedim, dims... >( );
}
template < int dim, int spacedim, int max_topo_dim_ >
auto get_topology_info( )
{
return get_topology_info_imp_< dim, spacedim >(
std::make_index_sequence< max_topo_dim_ >{} );
}
如果我这样使用它就可以了:
auto t = get_topology_info< 3, 3, 3 >( );
t 的类型是 TopologyInfo<3, 3, 0, 1, 2>,这是正确的。
那么现在的问题是:如何在不使用auto 的情况下生成t 的类型,这样我就可以将有问题的类作为另一个类的成员使用?
在我看来,我并不完全理解 std::index_sequence 背后的机制,而且解决方案应该是显而易见的。
【问题讨论】:
-
decltype(get_topology_info< 3, 3, 3 >( )) -
嗯,这确实有效。我使用
decltype来检查类型,但我从没想过我可以这样使用它。非常感谢。
标签: c++ templates variadic-templates