gpt4 book ai didi

c++ - 在另一个类主体中具有 variadict 模板的类的类型

转载 作者:行者123 更新时间:2023-12-03 07:09:43 26 4
gpt4 key购买 nike

我不是模板元编程方面的专家,我现在有点卡住了。任何帮助,将不胜感激。
作为介绍。
我有一个类(这里有点简化):

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>哪个是对的。
那么现在的问题是:如何生成 t的类型不使用 auto ,所以我将能够使用有问题的类(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;
};

关于c++ - 在另一个类主体中具有 variadict 模板的类的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64526291/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com