class 的类型在另一个 class 正文中具有 variadict 模板

Type of a class that has variadict template in another class body

我不是模板元编程方面的专家,我现在有点卡住了。任何帮助将不胜感激。

作为介绍。 我有一个class(这里有点简化):

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 的类型,这样我就可以将有问题的 class 作为另一个的成员使用class? 在我看来,我没有完全理解 std::index_sequence 背后的机制,解决方案应该是显而易见的。

auto 只是实际类型的占位符。当你写

auto t = get_topology_info< 3, 3, 3 >( );

然后 t 是某种特定类型。您使用 auto 的事实不会改变这一点。

如果实际类型太难拼写或不易识别,您也可以使用 auto 的堂兄 decltype。例如这与上面相同:

decltype( get_topology_info< 3, 3, 3>( )) t = get_topology_info< 3, 3, 3>( );

或者如果您已经有一个实例:

decltype( t ) s = get_topology_info< 3, 3, 3>( );

对于 class 成员,您可能需要使用别名:

using some_meaningful_name = decltype( get_topology_info< 3, 3, 3>( ) );

然后

struct foo {
   some_meaningful_name bar;
};

只是 idclev 答案的扩展:

如果您在代码 中使用不同的值 大量重复使用 get_topology_info,那么您可能需要换行它自己的类型:

template <int A, dim, int spacedim, int max_topo_dim>
using some_meaningful_name = decltype(get_topology_info<dim, spacedim, max_topo_dim>());

所以现在我们可以说:

some_meaningful_name<3,3,3> member