提供受保护模板成员函数的公共专业化

use*_*643 4 c++ c++11

我想做以下事情:

class Foo {
protected:
  template<Param>
  void operator()(const Param& param) {
     // stuff involving some RTTI magic
  }
public:
  void operator()(const A& param) should be operator()<A>;
  void operator()(const B& param) should be operator()<B>;
}
Run Code Online (Sandbox Code Playgroud)

基本上,我有一个泛型运算符(),它采用通用模板参数.但是,我只想发布类型安全的特定专业.

谢谢!

Ker*_* SB 6

只需为私有函数指定一个不同的名称:

 class Foo
 {
 private:
     template <typename T> void foo(const T &);

 public:
     void operator()(const A & x) { foo(x); }
     void operator()(const B & x) { foo(x); }
 };
Run Code Online (Sandbox Code Playgroud)