如何在不使用“运算符”一词的情况下调用模板化运算符重载?
class RunAround;
class HopUpAndDown;
class Sleep;
template<typename Acts> int doThis();
template<> int doThis<RunAround>() { /* run run run.. */ return 3; }
template<> int doThis<HopUpAndDown>() { /* hop hop hop.. */ return 2; }
template<> int doThis<Sleep>() { /* zzz.. */ return -2; }
struct Results
{
template<typename Act> int& operator()()
{
static int result;
return result;
}
};
int main()
{
Results results;
//results<RunAround>() = doThis<RunAround>();
results.operator ()<RunAround>() = doThis<RunAround>();
results.operator ()<Sleep>() = doThis<Sleep>();
return 0;
};
如果我删除注释,当我想要 operator
在 Results
类中。
如果我想继续使用运算符重载而不是普通名称,我是否注定要在注释下面使用糟糕的语法(这确实有效)?
class RunAround;
class HopUpAndDown;
class Sleep;
template<typename Acts> int doThis();
template<> int doThis<RunAround>() { /* run run run.. */ return 3; }
template<> int doThis<HopUpAndDown>() { /* hop hop hop.. */ return 2; }
template<> int doThis<Sleep>() { /* zzz.. */ return -2; }
struct Results
{
template<typename Act> int& operator()()
{
static int result;
return result;
}
};
int main()
{
Results results;
//results<RunAround>() = doThis<RunAround>();
results.operator ()<RunAround>() = doThis<RunAround>();
results.operator ()<Sleep>() = doThis<Sleep>();
return 0;
};
If I remove the comment, the compiler thinks I am calling operator()
in non-existant template class Results<RunAround>
when I want operator<RunAround>()
in class Results
.
If I want to continue using an operator overload instead of a normal name, am I doomed to use the awful syntax below the comment (which does work)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
最舒服的事情是让模板参数推导为您工作:
The most comfortable thing is to let template argument deduction work for you: