【问题标题】:Specialization of Notype template of type enum- error: template argument is invalid枚举类型的 Notype 模板的特化 - 错误:模板参数无效
【发布时间】:2015-06-22 09:40:09
【问题描述】:

我有这样的代码:

  enum GeneratorType
  {
    FILE_LIST, DEVICE
  };

  template<typename SceneT, int TYPE>
  class ODFrameGenerator
  {
  //... some APIS
  };

  template<typename SceneT>
  class ODFrameGenerator<SceneT, GeneratorType::DEVICE>
  {
  //...specialization: ERROR: template argument 2 is invalid
  };

  template<typename SceneT>
  class ODFrameGenerator<SceneT, 1>
  {
  //...specialization: compiles fine!!
  };

我尝试将定义中的template&lt;typename SceneT, int TYPE&gt; 更改为template&lt;typename SceneT, GeneratorType TYPE&gt;,但ist 仍然给出完全相同的错误。知道出了什么问题以及如何避免这种情况吗?

注意:这是用 c++11 编译的(带有 -std=c++11 标志);但否则失败。我正在使用 gcc 4.9.2。

编辑:我得到的确切错误如下:

/home/sarkar/opendetection/common/utils/ODFrameGenerator.h:80:61: error: template argument 2 is invalid
   class ODFrameGenerator<ODSceneImage, GeneratorType::DEVICE>
                                                             ^
/home/sarkar/opendetection/common/utils/ODFrameGenerator.h:100:46: error: wrong number of template arguments (1, should be 2)
   class ODFrameGenerator<ODScenePointCloud<> >, GeneratorType::DEVICE>
                                              ^
/home/sarkar/opendetection/common/utils/ODFrameGenerator.h:28:9: error: provided for ‘template<class SceneT, int TYPE> class od::ODFrameGenerator’
   class ODFrameGenerator
         ^
/home/sarkar/opendetection/examples/objectdetector/od_image_camera.cpp: In function ‘int main(int, char**)’:
/home/sarkar/opendetection/examples/objectdetector/od_image_camera.cpp:28:67: error: template argument 2 is invalid
   od::ODFrameGenerator<od::ODSceneImage, od::GeneratorType::DEVICE> frameGenerator("0");
                                                                   ^
/home/sarkar/opendetection/examples/objectdetector/od_image_camera.cpp:28:83: error: invalid type in declaration before ‘(’ token
   od::ODFrameGenerator<od::ODSceneImage, od::GeneratorType::DEVICE> frameGenerator("0");

【问题讨论】:

  • 这是您得到的 exact 错误吗?它完整吗?还有未编辑
  • @JoachimPileborg,我添加了我得到的确切错误。

标签: c++ templates c++11 enums template-specialization


【解决方案1】:

你混合了一些东西:如果你想使用枚举作为非类型模板参数,你需要在模板声明中指定它,如下所示:

template<typename SceneT, GeneratorType TYPE>
class ODFrameGenerator
{
  //... some APIS
};

现在,只要您使用 Device 而不是 GeneratorType::Device,一切都可以正常工作。要使用后一种形式,您需要将 GeneratorType 声明为枚举类

enum class GeneratorType
{
    FILE_LIST, DEVICE
};

【讨论】:

  • 从 C++11 开始,允许通过 enum_name::enumerator 命名 unscoped 枚举的枚举器。 scoped 枚举 (enum class) 不是必需的。当然,这并不意味着作用域枚举没有其他好处。
  • 谢谢,使用 DEVICE 代替 GeneratorType::DEVICE 解决了这个问题。现在,当我使用 class ODFrameGenerator&lt;CloudScene&lt;&gt; &gt;, DEVICE&gt; 之类的模板类实例化专门 SceneT 时,它会显示 wrong number of template arguments (1, should be 2)(问题中完整错误列表中的错误 #2)。知道有什么问题吗?
  • 我的错,第二个错误是由于其他原因。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 2010-12-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-10
  • 2023-04-03
相关资源
最近更新 更多