【问题标题】:How to alias a nested template class with variadic parameter packs如何使用可变参数包为嵌套模板类起别名
【发布时间】:2017-08-06 12:02:39
【问题描述】:

有什么方法可以为嵌套模板类加上 using 关键字的别名?像这样的

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;
}

此答案template template alias to a nested template? 显示了一种方法,但如果两个参数包都是可变参数,则该方法将不再有效,因为编译器将不知道从哪里开始以及在哪里结束类型列表。

【问题讨论】:

  • 为什么需要这种特定的方式?您可以稍微更改代码并使用类似的代码Something_t&lt;int&gt;::Something2&lt;double&gt;
  • @LmTinyToon 没有它是可能的,但我只是想知道它是否是一件事。
  • @LmTinyToon 因为他很好奇...:P
  • 我不知道别名,但您可以通过使用分隔符类型使用老式 struct-with-typedef 构建类似的东西。
  • @cdhowie 你的意思是类型列表之间的分隔符来分隔它们?

标签: c++ c++11 templates c++14 variadic-templates


【解决方案1】:

不完全是您所要求的,但是...如果您可以将可变参数类型列表包装为元组(或类似类)的参数...

#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>>{};
 }

【讨论】:

    【解决方案2】:

    不是独立的答案,而是 max66 答案的补充:

    你可以尝试这个:

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

    一开始看起来很不错,不是吗?

    那么问题就出在一个模板参数上:

    Alias<int> a;
    

    现在是哪一个? Something&lt;int&gt;::Another&lt;&gt; 还是 Something&lt;&gt;::Another&lt;int&gt;?如果你有更多的参数,如何分配?没有机会获得有意义的解决方案。所以不,你不能直接这样做,你必须通过诸如 max66 提议的技巧来帮助自己......

    【讨论】:

    • 他尝试了错误的语法(两个模板参数列表)——确定 that 不能工作。上面现在是正确的语法(与 max66 的 anwwer 相比),但由于歧义仍然无法工作......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多