The following code snippet works for me:
class Foo {
public:
template <class T> T& get () { ... }
};
Foo foo;
foo.get<int>() = ...;
However, the following code snippet does not work for me:
class Foo {
public:
template <class T> T& operator() () { ... }
};
Foo foo;
foo<int>() = ...;
The errors being:
expected primary-expression before '>' token
expected primary expression before ')' token
Both errors refer to the foo<int>()
Why does this not work and is it possible to fix this?
If you need to explicitly specify the template argument, you would need to use the
operatorsyntax:There isn’t any way to specify the arguments using the function-call syntax. If you can’t deduce the template arguments from the arguments to the function, it’s better to use a member function than an operator overload.