operator()重载模板C++

7 c++ templates overloading

我有一个简单的类,我想重载运算符,如下所示

class MyClass
{
   public:
      int first;

      template <typename T>
      T operator () () const { return first; }  
};
Run Code Online (Sandbox Code Playgroud)

还有我所拥有的其他地方

MyClass obj;

int i = obj(); // This gives me an error saying could not deduce
               // template argument for T
Run Code Online (Sandbox Code Playgroud)

有人可以帮我解决这个错误,非常感谢.谢谢.

编辑:

这与operator()有关,例如,如果我替换函数

    template <typename T>
    T get() const { return first;}
Run Code Online (Sandbox Code Playgroud)

有用.感谢所有回复.

the*_*row 5

如果您希望函数调用是隐式的,那么您必须将模板应用于类,如下所示:

template <typename T>
class MyClass
{
  public:
  T first;

  T operator () () const { return first; }  
};
Run Code Online (Sandbox Code Playgroud)

如果它应该被转换为另一种类型,那么它应该是:

template <typename T>
class MyClass
{
  public:
  T first;

  template <typename U>
  U operator () () const { return (U)first; }  
};
Run Code Online (Sandbox Code Playgroud)