【问题标题】:How to expand parameter pack for dependent types? [duplicate]如何为依赖类型扩展参数包? [复制]
【发布时间】:2020-08-13 21:04:07
【问题描述】:

在我想要的类型直接依赖于包中的类型的情况下,扩展参数包的语法是什么?

例如:

template <typename T>
struct foo
{
   typedef T value_type;
};

template <typename ... T>
struct aggregate_of_foo
{
   typedef std::tuple<foo<T>::value_type...> tuple; // MSVC compiler error here
};

【问题讨论】:

  • 你不想typedef std::tuple&lt;typename foo&lt;T&gt;::value_type...&gt; tuple;
  • 无关:不能只是using tuples = std::tuple&lt;T...&gt;;??不明白为什么需要foovalue_type
  • @JeJo 在这个人为的例子中,是的。但我的实际代码更复杂,不遵循foo&lt;T&gt;::value_typeT 相同的模式。

标签: c++ c++11 templates variadic-templates class-template


【解决方案1】:

如果您在 typedef 中指的是 foo 而不是 bar,那么您只是缺少之前的 typename 关键字:

template <typename ... T>
struct aggregate_of_foo
{
   using tuple_foo =  std::tuple<typename foo<T>::value_type...>;
                      //         ^^^^^^^^
};

另请注意,我将其命名为 tuple_foo,因为这比仅使用 tuple 更有意义。

【讨论】:

    【解决方案2】:

    您必须使用关键字typename,因为value_type 在此上下文中是一个从属名称。

    template <typename ... T>
    struct aggregate_of_foo
    {
        typedef std::tuple<typename foo<T>::value_type...> tuple; //here
    };
    

    【讨论】:

      猜你喜欢
      • 2018-02-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多