Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/139.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 将模板类实例的std::variant转换为std::tuple_C++_C++17 - Fatal编程技术网

C++ 将模板类实例的std::variant转换为std::tuple

C++ 将模板类实例的std::variant转换为std::tuple,c++,c++17,C++,C++17,下面代码中的transform\u v2t函数构建模板类a实例的元组: template <typename T> struct A { T val; }; template <class V, template <class> class T, std::size_t... index> inline constexpr auto transform_v2t(std::index_sequence<index...>) { re

下面代码中的
transform\u v2t
函数构建模板类a实例的元组:

template <typename T>
struct A
{
    T val;
};

template <class V, template <class> class T, std::size_t... index>
inline constexpr auto transform_v2t(std::index_sequence<index...>)
{
    return std::make_tuple(T<std::variant_alternative_t<index, V>>() ...);
}

template <class V, template <class> class T>
inline constexpr auto transform_v2t()
{
    return transform_v2t<V, T>(std::make_index_sequence<std::variant_size_v<V>>());
}

typedef std::variant<bool, char, int, float, double, std::string> V;

int main()
{
    auto t1 = transform_v2t<V, A>();
}
p专用于
int

对于伪代码,它可以是这样的:

template <class T> typedef B<int, T> PartiallySpecializedB;
auto t2 = transform_v2t<V, PartiallySpecializedB>();
模板类型def B PartiallySpecializedB;
自动t2=变换_v2t();

请参阅。

切勿在post-C++11代码中使用
typedef
,始终更喜欢使用
(称为别名声明)

它们不仅更容易阅读,因为您要声明的名称位于左侧(而不是…任何地方):

使用V=std::variant;
。。。但它们也支持别名模板声明:

template <class T> 
using PartiallySpecializedB = B<int, T>;

auto t2 = transform_v2t<V, PartiallySpecializedB>();
模板
使用PartiallySpecializedB=B;
自动t2=变换_v2t();

typedef int a;'和
使用A=int;`@AlexeyStarinsky在语义学方面?不,在可读性方面?对
using V = std::variant<bool, char, int, float, double, std::string>;
template <class T> 
using PartiallySpecializedB = B<int, T>;

auto t2 = transform_v2t<V, PartiallySpecializedB>();