Why can#39;t functions be overloaded by return type?(为什么函数不能被返回类型重载?)
问题描述
可能的重复:
按返回类型重载函数?
谜题:根据返回值重载一个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();
编译器现在做什么?
这篇关于为什么函数不能被返回类型重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么函数不能被返回类型重载?


基础教程推荐
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07