简体   繁体   English

如何使用可变参数包为嵌套模板类设置别名

[英]How to alias a nested template class with variadic parameter packs

Is there any way one can alias a nested template class with a using keyword? 有没有办法可以using关键字为嵌套模板类设置别名? Something like this 像这样的东西

template <typename... Types>
struct Something {
    template <typename... TypesTwo>
    struct Another {};
};

template <typename... Types>
template <typename... TypesTwo>
using Something_t = typename Something<Types...>::template Another<TypesTwo...>;

int main() {
    Something_t<int><double>{};
    return 0;
}

This answer template template alias to a nested template? 这个答案模板模板是嵌套模板的别名吗? shows a way to do that but that will no longer work if both the parameter packs are variadic, as the compiler will not know where to start and where to end the type lists. 显示了一种方法,但如果两个参数包都是可变参数,那么它将不再有效,因为编译器不知道从哪里开始以及在何处结束类型列表。

Not exactly what you asked but... if you can wrap your variadic type lists as arguments of tuples (or similar classes)... 不完全是你问的但是......如果你可以将你的可变参数类型列表包装为元组(或类似类)的参数......

#include <tuple>

template <typename ... Types>
struct Something
 {
   template <typename ... TypesTwo>
   struct Another {};
 };

template <typename, typename>
struct variadicWrapper;

template <typename ... Ts1, typename ... Ts2>
struct variadicWrapper<std::tuple<Ts1...>, std::tuple<Ts2...>>
 { using type = typename Something<Ts1...>::template Another<Ts2...>; };

template <typename T1, typename T2>
using Something_t = typename variadicWrapper<T1, T2>::type;

int main()
 {
   Something_t<std::tuple<int>, std::tuple<double>>{};
 }

Not a standalone answer, but an addition to max66's answer: 不是一个独立的答案,而是max66答案的补充:

You could have tried this: 你本可以尝试这个:

template<typename ... TT, typename ... TTT>
using Alias = typename Something<TT...>::Another<TTT...>;

Looks really nice at first, doesn't it? 起初看起来真的很好,不是吗?

Problem then, however will already be with one single template parameter: 然而问题是,已经有一个单一的模板参数:

Alias<int> a;

Which one is it now? 现在是哪一个? Something<int>::Another<> or Something<>::Another<int> ? Something<int>::Another<>Something<>::Another<int> And if you have more parameters, how to distribute? 如果您有更多参数,如何分发? No chance to get a meaningful solution. 没有机会获得有意义的解决方案。 So no, you can't do that directly, you have to help yourself with tricks such as max66 proposed... 所以不,你不能直接这样做,你必须帮助自己像max66提出的技巧......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM