模板别名难度
Template Alias Difficulty
我有以下内容:
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename... Accumulated, typename... Added, template <typename, T...> class Z, T... Is,
std::size_t... Js>
struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
Generate<T, End, Count + 1, typename Merge<P, P<Accumulated...>,
typename AppendEachToPack<T, P, Added, Is...>::type...>::type,
typename Merge<P, typename AppendEachToPack<T, P, Added, Is...>::type...>::type,
Z<T, Is...>, Js...> {};
并且因为 typename AppendEachToPack<T, P, Added, Is...>::type...
被计算了两次,我想将上面的重写为
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename... Accumulated, typename... Added, template <typename, T...> class Z, T... Is,
std::size_t... Js>
struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
GenerateAlias<T, End, Count + 1, P, P<Accumulated...>, Z<T, Is...>,
typename AppendEachToPack<T, P, Added, Is...>::type..., Js...> {};
哪里
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename Output, typename Sequence, typename... Expanded, std::size_t... Js>
using GenerateAlias = Generate<T, End, Count, typename Merge<P, Output, Expanded...>::type,
typename Merge<P, Expanded...>::type, Sequence, Js...>;
但不接受二包报关typename... Expanded, std::size_t... Js
。那么我该如何实现我想要实现的目标呢?
如果你需要,这里是AppendToEachPack
和Merge
的定义:
// Appending an element to a pack.
template <typename T, T t, typename> struct Append;
template <typename T, T t, template <T...> class Z, T... Is>
struct Append<T, t, Z<Is...>> {
using type = Z<Is..., t>;
};
// Appending many elements to a pack one at a time.
template <typename T, template <typename...> class P, typename Pack, T... Is>
struct AppendEachToPack {
using type = P<typename Append<T, Is, Pack>::type...>;
};
// Merging multiple packs of types into a single pack of types.
template <template <typename...> class P, typename... Packs> struct Merge;
template <template <typename...> class P, typename Pack>
struct Merge<P, Pack> {
using type = Pack;
};
template <template <typename...> class P, typename... Ts, typename... Us>
struct Merge<P, P<Ts...>, P<Us...>> {
using type = P<Ts..., Us...>;
};
template <template <typename...> class P, typename Pack1, typename Pack2, typename... Packs>
struct Merge<P, Pack1, Pack2, Packs...> {
using type = typename Merge<P, Pack1, typename Merge<P, Pack2, Packs...>::type>::type;
};
感谢 T.C. 的提示,这里有一个解决方案(经测试可以正常工作):
// Helper struct to forward the pack wrapping the problem pack.
template <template <typename...> class P, typename PackOfPacks, typename... OtherPacks>
struct MergeMany;
template <template <typename...> class P, typename... Packs, typename... OtherPacks>
struct MergeMany<P, P<Packs...>, OtherPacks...> : Merge<P, Packs..., OtherPacks...> {};
// Now the desired GenerateAlias.
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename Output, typename Sequence, typename Expanded, std::size_t... Js>
using GenerateAlias = Generate<T, End, Count, typename MergeMany<P, Expanded, Output>::type,
typename MergeMany<P, Expanded>::type, Sequence, Js...>;
// And now using GenerateAlias, so typename AppendEachToPack<T, P, Added, Is...>::type... is computed only once.
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P, typename... Accumulated,
typename... Added, template <typename, T...> class Z, T... Is, std::size_t... Js>
struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
GenerateAlias<T, End, Count + 1, P, P<Accumulated...>, Z<T, Is...>,
P<typename AppendEachToPack<T, P, Added, Is...>::type...>, Js...> {};
我有以下内容:
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename... Accumulated, typename... Added, template <typename, T...> class Z, T... Is,
std::size_t... Js>
struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
Generate<T, End, Count + 1, typename Merge<P, P<Accumulated...>,
typename AppendEachToPack<T, P, Added, Is...>::type...>::type,
typename Merge<P, typename AppendEachToPack<T, P, Added, Is...>::type...>::type,
Z<T, Is...>, Js...> {};
并且因为 typename AppendEachToPack<T, P, Added, Is...>::type...
被计算了两次,我想将上面的重写为
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename... Accumulated, typename... Added, template <typename, T...> class Z, T... Is,
std::size_t... Js>
struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
GenerateAlias<T, End, Count + 1, P, P<Accumulated...>, Z<T, Is...>,
typename AppendEachToPack<T, P, Added, Is...>::type..., Js...> {};
哪里
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename Output, typename Sequence, typename... Expanded, std::size_t... Js>
using GenerateAlias = Generate<T, End, Count, typename Merge<P, Output, Expanded...>::type,
typename Merge<P, Expanded...>::type, Sequence, Js...>;
但不接受二包报关typename... Expanded, std::size_t... Js
。那么我该如何实现我想要实现的目标呢?
如果你需要,这里是AppendToEachPack
和Merge
的定义:
// Appending an element to a pack.
template <typename T, T t, typename> struct Append;
template <typename T, T t, template <T...> class Z, T... Is>
struct Append<T, t, Z<Is...>> {
using type = Z<Is..., t>;
};
// Appending many elements to a pack one at a time.
template <typename T, template <typename...> class P, typename Pack, T... Is>
struct AppendEachToPack {
using type = P<typename Append<T, Is, Pack>::type...>;
};
// Merging multiple packs of types into a single pack of types.
template <template <typename...> class P, typename... Packs> struct Merge;
template <template <typename...> class P, typename Pack>
struct Merge<P, Pack> {
using type = Pack;
};
template <template <typename...> class P, typename... Ts, typename... Us>
struct Merge<P, P<Ts...>, P<Us...>> {
using type = P<Ts..., Us...>;
};
template <template <typename...> class P, typename Pack1, typename Pack2, typename... Packs>
struct Merge<P, Pack1, Pack2, Packs...> {
using type = typename Merge<P, Pack1, typename Merge<P, Pack2, Packs...>::type>::type;
};
感谢 T.C. 的提示,这里有一个解决方案(经测试可以正常工作):
// Helper struct to forward the pack wrapping the problem pack.
template <template <typename...> class P, typename PackOfPacks, typename... OtherPacks>
struct MergeMany;
template <template <typename...> class P, typename... Packs, typename... OtherPacks>
struct MergeMany<P, P<Packs...>, OtherPacks...> : Merge<P, Packs..., OtherPacks...> {};
// Now the desired GenerateAlias.
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P,
typename Output, typename Sequence, typename Expanded, std::size_t... Js>
using GenerateAlias = Generate<T, End, Count, typename MergeMany<P, Expanded, Output>::type,
typename MergeMany<P, Expanded>::type, Sequence, Js...>;
// And now using GenerateAlias, so typename AppendEachToPack<T, P, Added, Is...>::type... is computed only once.
template <typename T, std::size_t End, std::size_t Count, template <typename...> class P, typename... Accumulated,
typename... Added, template <typename, T...> class Z, T... Is, std::size_t... Js>
struct Generate<T, End, Count, P<Accumulated...>, P<Added...>, Z<T, Is...>, Js...> :
GenerateAlias<T, End, Count + 1, P, P<Accumulated...>, Z<T, Is...>,
P<typename AppendEachToPack<T, P, Added, Is...>::type...>, Js...> {};