【发布时间】:2020-04-21 02:40:50
【问题描述】:
最好用一个例子来解释:
template <typename T1, typename T2>
struct OnePair
{
using TupleOfArgs = std::tuple<T1, T2>;
using TupleOfPairs = std::tuple<std::pair<T1, T2>>;
};
template <typename T1, typename T2, typename T3, typename T4>
struct TwoPairs
{
using TupleOfArgs = std::tuple<T1, T2, T3, T4>;
using TupleOfPairs = std::tuple<std::pair<T1, T2>, std::pair<T3, T4>>;
};
template <typename... Args>
struct NPairs
{
using TupleOfArgs = std::tuple<Args...>;
// using TupleOfPairs = ???
};
OnePair 定义了一个包含一对的元组。 TwoPairs 定义了一个有两对的元组。
如何在 NPairs 中定义 TupleOfPairs,以便将参数包转换为 std::tuple of pairs?
是否可以使用 std 库来实现?也许用 boost::mpl?
两个答案,都很棒。 @chris 使用迭代方法,而 @aschepler 使用递归解决方案。 就个人而言,我发现递归解决方案更容易理解。
【问题讨论】:
标签: c++ template-meta-programming boost-mpl