枚举类型枚举模板的特殊化 - 错误:模板参数无效

时间:2015-06-22 09:40:09

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

我有这样的代码:

  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<typename SceneT, int TYPE>更改为template<typename SceneT, GeneratorType TYPE>,但仍然会给出完全相同的错误。知道什么是错的,怎么避免这个?

注意:这是用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");

1 个答案:

答案 0 :(得分:3)

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

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

如果你使用Device而非GeneratorType::Device,事情就可以了。要使用后一种形式,您需要将GeneratorType声明为枚举类

enum class GeneratorType
{
    FILE_LIST, DEVICE
};