Search code examples
c++templatesvariadic-templates

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


I am not an expert in template metaprogramming, and I am kind of stuck now. Any help would be appreciated.

As an introduction. I have a class (a bit simplified here):

template < int dim, int spacedim, int... structdims >
class TopologyInfo
{
}

I can create an instance of TopologyInfo with functions:

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_ >{} );
}

This works if I use it like that:

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

Type of t is TopologyInfo<3, 3, 0, 1, 2> which is correct.

So now the problem: how to generate the type of t without using auto, so I will be able to use the class in question as a member of the other class? It seems to me that I don't understand fully the mechanisms behind std::index_sequence and that the solution should be kind of obvious.


Solution

  • auto is just a placehold for the actual type. When you write

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

    then t is of some specific type. The fact that you used auto doesn't change that.

    If the actual type is too unwieldy to be spelled out or not readily known, you can also use autos cousin decltype. For example this is the same as the above:

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

    or if you already have an instance:

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

    For a class member you might want to use an alias:

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

    Then

    struct foo {
       some_meaningful_name bar;
    };