Specialization of templated member function in templated class

前端 未结 2 927
情歌与酒
情歌与酒 2020-11-28 14:32

I have a templated class with an templated member function

template
class A {
public:
    template
    CT function();
};


        
相关标签:
2条回答
  • 2020-11-28 15:00

    You can use overload, if you change the implementation.

    template <typename T>
    class Foo
    {
    public:
      template <typename CT>
      CT function() { return helper((CT*)0); }
    
    private:
      template <typename CT>
      CT helper(CT*);
    
      T helper(T*) { return (T)0.0; }
    
      bool helper(bool*) { return false; }
    };
    

    Simple and easy :)

    0 讨论(0)
  • 2020-11-28 15:04

    Yes, this is the problem:

    error: enclosing class templates are not explicitly specialized 
    

    You cannot specialize a member without also specializing the class.

    What you can do is put the code from function in a separate class and specialize that, much like basic_string depends on a separate char_traits class. Then then non-specialized function can call a helper in the traits class.

    0 讨论(0)
提交回复
热议问题