>::type是Pack, Pack > .但“外包装”不-6ren">
gpt4 book ai didi

c++ - 从一包嵌套包中提取每个 "leaf-pack"

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:19:24 26 4
gpt4 key购买 nike

ExtractEveryPack<Pack>::typePack 中所有“叶包”的包.例如,ExtractEveryPack< Pack<double, Pack<int, char>, int, Pack<long, short>> >::typePack< Pack<int, char>, Pack<long, short> > .但“外包装”不退还。仅提取最内层的包(我称之为“叶包”)。所以

ExtractEveryPack<Pack<Pack<int, double>, char, Pack<long, Pack<Pack<int, char>, Pack<char, Pack<double, int>>>, char>, int, Pack<short, int>>>::type,

Pack< Pack<int, double>, Pack<int, char>, Pack<double, int>, Pack<short, int>.

我的想法:ExtractEveryPack<T>::typeT默认情况下。然后递归地应用 ExtractEveryPack对于每种类型并删除所有不是包的类型:

#include <iostream>

template <typename, typename> struct RemoveNonPacksHelper;

template <template <typename...> class P, typename... Accumulated>
struct RemoveNonPacksHelper<P<>, P<Accumulated...>> {
using type = P<Accumulated...>;
};

template <template <typename...> class P, typename First, typename... Rest, typename... Accumulated>
struct RemoveNonPacksHelper<P<First, Rest...>, P<Accumulated...>> : RemoveNonPacksHelper<P<Rest...>, P<Accumulated...>> {};

template <template <typename...> class P, typename... Types, typename... Rest, typename... Accumulated>
struct RemoveNonPacksHelper<P<P<Types...>, Rest...>, P<Accumulated...>> : RemoveNonPacksHelper<P<Rest...>, P<Accumulated..., P<Types...>>> {};

template <typename> struct RemoveNonPacks;

template <template <typename...> class P, typename... Types>
struct RemoveNonPacks<P<Types...>> : RemoveNonPacksHelper<P<Types...>, P<>> {};

template <typename T> struct Identity { using type = T; };

template <typename T>
struct ExtractEveryPack : Identity<T> {}; // Do nothing for non-packs.

// The key idea here, but apparently not correct:
template <template <typename...> class P, typename... Types>
struct ExtractEveryPack<P<Types...>> :
RemoveNonPacks<P<typename ExtractEveryPack<Types>::type...>> {};

// Testing
template <typename...> struct Pack {};

int main() {
std::cout << std::boolalpha << std::is_same<
RemoveNonPacks< Pack<Pack<int, double>, char, Pack<long, double, char>, int, Pack<short, int>> >::type,
Pack<Pack<int, double>, Pack<long, double, char>, Pack<short, int>>
>::value << std::endl; // true

std::cout << std::is_same<
ExtractEveryPack<Pack<Pack<int, double>, char, Pack<long, Pack<Pack<int, char>, Pack<char, Pack<double, int>>>, char>, int, Pack<short, int>>>::type,
Pack< Pack<int, double>, Pack<int, char>, Pack<double, int>, Pack<short, int> >
>::value << std::endl; // false (darn!)
}

这是怎么回事?我的计划或实现呢?什么是更好的计划?

就其值(value)而言,这是一个辅助结构 IsLeafPack确定一个包是否不包含其他包(已测试),尽管我还没有想出如何使用它:

template <typename> struct IsLeafPack;

template <template <typename...> class P>
struct IsLeafPack<P<>> : std::true_type {};

template <template <typename...> class P, template <typename...> class P2, typename... Types, typename... Rest>
struct IsLeafPack<P<P2<Types...>, Rest...>> : std::false_type {};

template <template <typename...> class P, typename First, typename... Rest>
struct IsLeafPack<P<First, Rest...>> : IsLeafPack<P<Rest...>> {};

最佳答案

递归是正确的。但是应用 ExtractEveryPack 的结果可以是任意数量(包括 0)的叶包。因此,它必须返回一组类型,而不是返回单一类型。然后可以将这些包连接起来以产生最终输出。

// A pack template.
template <typename...> struct Pack {};

// Test if Ts... contains any pack - not necessarily a Pack.
template <typename... Ts>
struct contains_any_pack : std::false_type {};

template <template <typename...> class P, typename... TPs, typename... Ts>
struct contains_any_pack<P<TPs...>, Ts...> : std::true_type {};

template <class F, typename... Ts>
struct contains_any_pack<F, Ts...> : contains_any_pack<Ts...> {};

// concatenates a list of Pack's into one Pack.
template <typename... Ts> struct concat_packs;

template <typename... Ts>
struct concat_packs<Pack<Ts...>> { using type = Pack<Ts...>; };

template <typename... Ts, typename... T1s, typename... T2s>
struct concat_packs<Pack<Ts...>, Pack<T1s...>, T2s... >
: concat_packs<Pack<Ts..., T1s...>, T2s... > {};


// T isn't a pack - return an empty Pack
template <typename T>
struct ExtractEveryPack { using type = Pack<>; };

// if P<Ts...> is a leaf pack, return it wrapped in a Pack.
// else, apply ExtractEveryPack to Ts... recursively,
// and concatenate the results
template <template <typename...> class P, typename... Ts>
struct ExtractEveryPack<P<Ts...>> {
using type = typename std::conditional<contains_any_pack<Ts...>::value,
typename concat_packs<typename ExtractEveryPack<Ts>::type...>::type,
Pack<P<Ts...>>>::type;
};

为了简化实现,上面的 ExtractEveryPack 总是返回叶包的 Pack。然而,叶子包的类型并不限于 Pack

关于c++ - 从一包嵌套包中提取每个 "leaf-pack",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28678768/

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