实例化模板参数的参数包

Bom*_*maz 4 c++ gcc visual-c++ variadic-templates c++17

我想制作一个模板,它接受一组模板并使用相同的参数包实例化它们。

不幸的是,我似乎无法弄清楚如何在模板参数包中扩展参数包。

我怎样才能使这个编译?

#include<type_traits>
#include <tuple>

template <template <typename...> typename... Args>
struct TupleTupleMaker
{
    template <typename... InstantiateWith>
    using NewTupleTuple = typename std::tuple<Args<InstantiateWith...>...>;
};

template<typename a, typename b>
using tuple1 = std::tuple<int,a,b>;

template<typename a, typename b>
using tuple2 = std::tuple<a,b,b>;

using expected = std::tuple<
    std::tuple<int,int,double>,
    std::tuple<int,double,double>>;

using actual = TupleTupleMaker<tuple1,tuple2>::NewTupleTuple<int,double>;

static_assert(std::is_same_v<actual,expected>, "Should be same");
Run Code Online (Sandbox Code Playgroud)

T.C*_*.C. 5

根据核心问题 1430的方向,您不能将 expand 打包为具有固定参数列表的别名模板。解决方法是重写tuple1tuple2通过类模板路由它们:

template<class a, class b>
struct tuple1_impl { using type = std::tuple<int,a,b>; };

template<typename... a>
using tuple1 = typename tuple1_impl<a...>::type;

template<class a, class b>
struct tuple2_impl { using type = std::tuple<a,b, b>; };

template<typename... a>
using tuple2 = typename tuple2_impl<a...>::type;
Run Code Online (Sandbox Code Playgroud)