我有一个元组
using MyTuple = std::tuple<Foo1, Foo2, Foo3, ... FooN>;
并想像这样构建 MyTuple
MyTuple foo{Foo1{x}, Foo2{x}, Foo3{x}, ... FooN{x}};
FooK
不是默认可构造的N
在写入时未知。因此,我无法对上面的列表进行硬编码。
我所拥有的是
Enum::Tuple<EnumType, EnumTypeTraits> foo;
这是根据这里的建议构建的:Create template pack from set of traits,如下所示:https ://github.com/milasudril/libenum/blob/master/libenum/tuple.hpp 。
我是否需要添加一些 ctor 才能Enum::Tuple
使其工作?
这里Enum::Tuple
供参考:
namespace detail
{
template<ContiguousEnum EnumType,
template<EnumType>
class EnumItemTraits,
class T = std::make_integer_sequence<
std::underlying_type_t<EnumType>,
Size<EnumType>::value // NOTE: calling distance here triggers ICE in gcc 10.2
>>
struct make_tuple;
template<ContiguousEnum EnumType,
template<EnumType>
class EnumItemTraits,
std::underlying_type_t<EnumType>... indices>
struct make_tuple<EnumType,
EnumItemTraits,
std::integer_sequence<std::underlying_type_t<EnumType>, indices...>>
{
using type =
std::tuple<typename int_to_type<EnumType, EnumItemTraits, indices>::type...>;
};
}
template<ContiguousEnum EnumType, template<EnumType> class EnumItemTraits>
class Tuple: private detail::make_tuple<EnumType, EnumItemTraits>::type
{
public:
using Base = typename detail::make_tuple<EnumType, EnumItemTraits>::type;
using index_type = EnumType;
template<index_type index>
using traits = EnumItemTraits<index>;
using Base::Base;
static constexpr auto size() { return std::tuple_size_v<Base>; }
template<index_type i>
using tuple_element = std::tuple_element_t<distance(begin(Empty<EnumType>{}), i), Base>;
template<index_type i>
constexpr auto const& get() const
{
return std::get<distance(begin(Empty<EnumType>{}), i)>(base());
}
template<index_type i>
constexpr auto& get()
{
return std::get<distance(begin(Empty<EnumType>{}), i)>(base());
}
private:
Base const& base() const { return *this; }
Base& base() { return *this; }
};