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>::type默认为T。然后,递归地将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/

10-11 23:02