从具有可变参数的模板类继承

时间:2017-12-12 12:41:35

标签: c++ templates variadic-templates

我有这段代码:

  template<char ...T>
  class base
  {
       std::array<uint8_t, ID_SIZE> m_ID = { Ts... };
  }

  template<char ...T>
  class derived: public base<T>
  {
       // this class doesn;t need to know anything about T
  }

当我编译此代码时,我收到此错误:

  'T': parameter pack must be expanded in this context  

这是什么错误以及如何解决?

2 个答案:

答案 0 :(得分:2)

T不是一种类型,它是“参数包”的名称。

base<T>是荒谬的,因为base需要一个类型列表,而不是一包类型。 base<T...>将解压缩类型并按预期工作。

答案 1 :(得分:2)

多个模板参数(类型或非类型)不能作为包传递,但每次都必须解压缩:

template<char ...T>
class base { }

template<char ...T>
class derived: public base<T...> // unpack
{      
}

base<>内,参数将在T的上下文中重新打包。