提供受保护的模板成员函数

时间:2016-05-23 09:27:20

标签: 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>;
}

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

谢谢!

1 个答案:

答案 0 :(得分: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); }
 };
相关问题