为什么函数不能被返回类型重载? [英] Why can't functions be overloaded by return type?

查看:16
本文介绍了为什么函数不能被返回类型重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
按返回类型重载函数?
谜题:根据返回值重载一个C++函数

因为我有一个库,它以以下形式公开了一堆函数:

Because I have a library which exposes a bunch of functions in the form of:

bool GetVal();
double GetVal();
int GetVal();
long GetVal();
//So on.

现在我必须包装这些.我宁愿不再重写同一组函数.我想做类似的事情

And now I have to wrap these. I'd rather not rewrite the same set of functions again. I'd like to do something like

template<class T>
T GetVal(){}

但我似乎无法让它工作.有什么想法吗?

But I can't seem to get this working. Any ideas?

推荐答案

您不能重载返回类型,因为在函数调用表达式中使用函数的返回值不是强制性的.

You can't overload on return types as it is not mandatory to use the return value of the functions in a function call expression.

例如,我可以说

GetVal();

编译器现在做什么?

这篇关于为什么函数不能被返回类型重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆