我想将一系列类存储到一个元组中,并将该序列声明为另一个类的成员:
template<size_t id> class Foo {};
template<size_t N>
class FooContainer
{
std::tuple<Foo<0>, Foo<1>, ..., Foo<N>> tup; // build this type at compile time ??
};
我试过这个:
template<size_t N>
class FooContainer
{
template<size_t... id>
struct FoosImpl {
constexpr FoosImpl(std::index_sequence<id...>) {};
using type = std::tuple<Foo<id>...>;
};
template<size_t N, typename Indices = std::make_index_sequence<N>>
using Foos = decltype(FoosImpl(Indices())::type);
Foos<N> tup;
};
但这并没有编译。海湾合作委员会投诉:error: missing template arguments before ‘(’ token using Foos = decltype(FoosImpl(Indices())::type);
我认为编译器不需要指定模板,并且它会从中推导出整数序列Indices()
。但事实似乎并非如此。
这是做你想做的事情的一种可能的方法:
#include <tuple>
template<size_t id> class Foo {};
template <size_t... Idx>
std::tuple<Foo<Idx>...> get_foos(std::index_sequence<Idx...>);
template <size_t N>
using foo_tuple = decltype(get_foos(std::make_index_sequence<N>{}));
template<size_t N>
class FooContainer {
foo_tuple<N> tup;
};
您(当前)不能让编译器推断出类模板参数(正如您需要的那样)FoosImpl
),但您可以让它推断函数的模板参数并使用返回类型。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)