从特征集创建模板包

问题描述

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

#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;

解决方法

您可以这样做:

#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
};

// replace NUMBER_OF_TYPES
template <typename T=std::make_index_sequence<NUMBER_OF_TYPES> >
struct make_my_variant;

template <size_t... indices>
struct make_my_variant<std::index_sequence<indices...> > {
    using type = std::variant<typename IntToType<indices>::type...>;
};

using MyVariant = typename std::make_my_variant<>::type;

请注意,要以字符串文字形式找到类型名称,可以只使用typeid(TYPE).name()。如果需要,您可能需要取消使用此名称的名称;您可以使用特定于编译器的demangler函数(我认为MSVC不会破坏类型名称,但是在GCC上,您可以在abi::__cxa_demangle标头中使用<cxxabi.h>。)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...