如何将元组<>剥离回可变参数类型列表中?

问题描述 投票:14回答:5

是否有方法strip std::tuple<T...>以使其恢复为T...

示例

假设vct<T...>预先存在 可变类模板

using U = std::tuple<int,char,std::string>;
using X = vct<int,char,std::string>;
using Y = vct< strip<U> >;            // should be same as X

Notes

我知道std::tuple_element,但是我需要所有元素,并且格式可以用作T...

作为参考,我发现了这个question,它很相似,但是我的需求稍微简单了(所以我希望有一个更简单的解决方案):我需要的只是tuple中的类型列表-我不在乎tuple实例的实际值。

c++ templates c++11 std variadic-templates
5个回答
12
投票

不,这是不可能的。参数包是类型推导的结果,它们不能在其他上下文中产生。

您可以通过这种方式执行与您要求的类似的事情:

template<template<typename...> class T, typename>
struct instantiate_with_arg_pack { };

template<template<typename...> class T, typename... Ts>
struct instantiate_with_arg_pack<T, std::tuple<Ts...>>
{
    using type = T<Ts...>;
};

template<typename... Ts>
struct vct { };

int main()
{
    using U = std::tuple<int,char,std::string>;
    using X = vct<int,char,std::string>;
    using Y = instantiate_with_arg_pack<vct, U>::type;
}

实际上,您不需要将参数包保存在元组中:任何可变参量类模板都可以:

template<template<typename...> class T, typename>
struct instantiate_with_arg_pack { };

template<
    template<typename...> class T, 
    template<typename...> class U, // <===
    typename... Ts
    >
struct instantiate_with_arg_pack<T, U<Ts...>>
//                                   ^^^^^^^^
{
    using type = T<Ts...>;
};

template<typename... Ts>
struct vct { };

int main()
{
    using U = std::tuple<int,char,std::string>;
    using X = vct<int,char,std::string>;
    using Y = instantiate_with_arg_pack<vct, X>::type;
    //                                        ^

    // Won't fire
    static_assert(
        std::is_same<Y, vct<int,char,std::string>>::value, 
        "Error!");
}

这里是live example


15
投票
template<typename>
struct strip;

template<typename ...T>
struct strip<std::tuple<T...>>
{
   using type = vct<T...>;
};

然后将其用作:

using Y = strip<U>::type;

现在YX相同。


4
投票

您不能直接“返回”参数包,所以您需要的是这样的东西:

template< typename... Ts >
struct vct
{ ... };

template< typename T >
struct make_vct;

template< typename... Ts >
struct make_vct< std::tuple< Ts... > >
{
    typedef vct< Ts... > type;
};

和使用

using Y = make_vct< U >::type;

2
投票

这里的目标是能够将参数包从模板的实例复制到另一个模板。我没有将其限制为tuple,因为...为什么将其限制为tuple

template<template<typename...>class Target, typename Src>
struct copy_pack_types;

template<template<typename...>class Target, template<typename...>class Src, typename... Ts>
struct copy_pack_types< Target, Src<Ts...> > {
  typedef Target<Ts...> type;
};

template<template<typename... Ts>class Target, typename Src>
using CopyPackTypes = typename copy_pack_types<Target, Src>::type;

#include <string>
#include <tuple>
template<typename... Ts> struct vct;
template<typename... Ts> struct vct2;
using U = std::tuple<int,char,std::string>;
using X = vct<int,char,std::string>;
using Y = CopyPackTypes< vct, U >;            // should be same as X
using Z = CopyPackTypes< vct2, U >;            // should be different from X

#include <iostream>
#include <type_traits>
int main() {
  std::cout << std::is_same< X, Y >::value << "\n";
  std::cout << std::is_same< Z, Y >::value << "\n";
  std::cout << std::is_same< Z, vct2<int,char,std::string> >::value << "\n";
}

输出为预期的“ 1 0 1”。

CopyPackTypes采用目标模板,以及从参数包构造为唯一参数的源类型。然后,它将参数包复制到目标模板。

一种标准技术是随身携带参数包,即创建一个其他的不使用类型,例如:

template<typename...>
struct types {};

仅作为类型列表的占位符存在。然后,您可以将其中一些传递给另一个模板,而每个包不会彼此“踩”。当您需要将其应用于目标模板时,可以使用上述“ CopyPackTypes”之类的东西来应用它。

类似的技术用于索引包:

template<size_t...>
struct seq {};

否则是无用的类型,它们是“黑板”,用于携带成群的参数。


0
投票

使用c ++ 17 std::apply

using U = std::tuple<int, char, std::string>;
using X = vct<int, char, std::string>;
using Y = vct<strip<U>>; // should be same as X

auto my_y = std::apply(
        [](auto... ts) {
            using Y = vct<decltype(ts)...>;
            return Y{};
        },
        U{});
© www.soinside.com 2019 - 2024. All rights reserved.