Understanding (simple?) C++ Partial Template Specialization

前端 未结 2 1504
礼貌的吻别
礼貌的吻别 2020-12-04 10:38

Note: this seems to be a repost of a problem: C++ - Overload templated class method with a partial specilization of that method

I have boiled down a

2条回答
  •  长情又很酷
    2020-12-04 11:10

    This is a very often found problem, and there is a surprisingly simple solution. I will show it in an artificial example, because it's more clearer than to use your code, and you will have to understand it to adapt it to your code

    template
    struct TwoTypes { };
    
    template
    struct X {
      /* forwards ... */
      void f() { fImpl(TwoTypes()); }
    
      /* special overload for  */
      template
      void fImpl(TwoTypes) {
        /* ... */
      }
    
      /* generic */
      template
      void fImpl(TwoTypes) {
        /* ... */
      }
    };
    

    Explicitly specializing functions is never (almost never?) the right way. In my work as a programmer, I've never explicitly specialized a function template. Overloading and partial ordering is superior.

提交回复
热议问题