6 c++ templates operator-overloading
以下代码段适用于我:
class Foo {
public:
template <class T> T& get () { ... }
};
Foo foo;
foo.get<int>() = ...;
Run Code Online (Sandbox Code Playgroud)
但是,以下代码段对我不起作用:
class Foo {
public:
template <class T> T& operator() () { ... }
};
Foo foo;
foo<int>() = ...;
Run Code Online (Sandbox Code Playgroud)
错误是:
expected primary-expression before '>' token
expected primary expression before ')' token
Run Code Online (Sandbox Code Playgroud)
这两个错误都指的是 foo<int>()
为什么这不起作用,是否可以解决这个问题?
如果需要显式指定模板参数,则需要使用以下operator语法:
foo.operator()<int>()
Run Code Online (Sandbox Code Playgroud)
没有任何方法可以使用函数调用语法指定参数.如果无法从函数的参数中推导出模板参数,则最好使用成员函数而不是运算符重载.
| 归档时间: |
|
| 查看次数: |
863 次 |
| 最近记录: |