实例化模板参数的参数包

时间:2018-07-03 10:06:36

标签: 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");

1 个答案:

答案 0 :(得分:4)

按照core issue 1430的方向,您不能将扩展打包到具有固定参数列表的别名模板中。解决方法是通过类模板路由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;