从特征集创建模板包

问题描述

是否有可能(如果可以,如何)从索引的特征类型集合中生成模板包,以便将其用于实例化变体或元组?

#include <variant>

template<int n>
struct IntToType;

template<>
struct IntToType<0>
{
    using type = int;
    static constexpr char const* name = "int";
//  Other compile-time metadata
};

template<>
struct IntToType<1>
{
    using type = double;
    static constexpr char const* name = "double";
//  Other compile-time metadata
};

using MyVariant = std::variant<IntToType<???>::type...>;  // something with make_integer_sequence and fold expression?

或者是否有必要使用变体作为输入:

#include <variant>

using MyVariant = std::variant<int,double>;

template<int n>
struct IntToTypeBase
{
    using type = std::variant_alternative_t<n,MyVariant>;
};

template<int >
struct IntToType;

template<>
struct IntToType<0>:IntToTypeBase<0>
{
    static constexpr char const* name = "int";
//  Other compile-time metadata
};

template<>
struct IntToType<1>:IntToTypeBase<1>
{
    static constexpr char const* name = "double";
//  Other compile-time metadata
};

甚至滚动自己的variant,它接受​​一组特征而不是简单的类型列表:

template<class IntegerType,template<auto> class Traits,size_t LastIndex>
class Variant;

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)