简体   繁体   English

免费功能的模板参数

[英]template parameters for free functions

I'm experimenting with template template parameters. 我正在尝试使用模板模板参数。 I have no problems to make them work with classes but for some reason it does not work with functions. 我没有问题可以使它们与类一起使用,但是由于某种原因,它与函数不兼容。

enum class MyEnum { A, B, C, D};

  template<class EnumType, template<EnumType> class Fun >
  class MyTest
    {
    }

    template<MyEnum myEnum>
      void freeFunc(int argument)
        {
        LOG_ERROR("  void freeFunc(int argument) default case!!! ");
        }

      template<>
      void freeFunc<MyEnum::A>(int argument); // implemented in cpp

      template<>
      void freeFunc<MyEnum::B>(int argument); // implemented in cpp

      template<>
      void freeFunc<MyEnum::C>(int argument); // implemented in cpp

      template<>
      void freeFunc<MyEnum::D>(int argument); // implemented in cpp


template<MyEnum s>
class Cde
  {
   public:
  };


  MyTest<MyEnum, Cde > test1; // does compile

  MyTest<MyEnum, freeFunc > test2; // does not compile

I don't understand why the test2 does not compile. 我不明白为什么test2无法编译。 It just says: error: expected a class template, got 'freeFunc' 它只是说:错误:需要一个类模板,得到了'freeFunc'

What am I doing wrong? 我究竟做错了什么?

[edit] what I'm looking for is a way to get a generic way to both get free functions and class template templatized on an enum class 我正在寻找的是一种获得通用方法的方法,该方法既可以获取免费的函数,又可以在枚举类上模板化类模板

Note that function templates can't be used as template template argument , 请注意,函数模板不能用作模板模板参数

A template argument for a template template parameter must be an id-expression which names a class template or a template alias. 模板模板参数的模板参数必须是ID表达式,用于命名类模板或模板别名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM