gpt4 book ai didi

c++ - 实例化模板参数的参数包

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:50:13 25 4
gpt4 key购买 nike

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

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

如何编译?

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

最佳答案

按照core issue 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;

关于c++ - 实例化模板参数的参数包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51151845/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com