【问题标题】:Is there a way to expand the scoped types of variadic template parameters in a parameter pack in c++? [duplicate]有没有办法在 C++ 的参数包中扩展可变参数模板参数的范围类型? [复制]
【发布时间】:2021-12-17 00:24:51
【问题描述】:

我想强制参数包中的所有类型都声明一个嵌套类型别名 (T),并将所有类型的 T 扩展为 tuple。这样的事情可能吗?当我尝试下面的幼稚方式时,它不会将Types::T 识别为一种类型。

class A { using T = int; };
class B { using T = double; };


template<class ... Types>
class C {
  using tuple_of_types_t        = std::tuple<Types...>;       // of course this works
  using tuple_of_nested_types_t = std::tuple<((Types::T),...)>;  // how do I achieve this?
};

【问题讨论】:

  • 使用typename:std::tuple&lt;typename Types::T...&gt;;when to use typename, template keywords
  • 请注意,AB 中的 T 都是私有的,这会影响类型检查
  • type 是比T 更常规的 typedef 命名。

标签: c++ templates c++17 variadic-templates parameter-pack


【解决方案1】:

需要通知编译器Types::T 应该被解释为一个类型

using tuple_of_nested_types_t = std::tuple<typename Types::T...>;
//                                         ^^^^^^^^

See it compile on Compiler Explorer

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-21
    • 1970-01-01
    • 2014-10-30
    • 2014-04-12
    • 1970-01-01
    • 2014-04-29
    相关资源
    最近更新 更多