Specialization of templated member function in templated class

早过忘川 提交于 2019-11-26 22:58:32

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.

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 :)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!