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();
编译器现在做什么?
这篇关于为什么函数不能被返回类型重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么函数不能被返回类型重载?


- 从python回调到c++的选项 2022-11-16
- 与 int by int 相比,为什么执行 float by float 矩阵乘法更快? 2021-01-01
- 使用/clr 时出现 LNK2022 错误 2022-01-01
- Stroustrup 的 Simple_window.h 2022-01-01
- 静态初始化顺序失败 2022-01-01
- STL 中有 dereference_iterator 吗? 2022-01-01
- 如何对自定义类的向量使用std::find()? 2022-11-07
- C++ 协变模板 2021-01-01
- 近似搜索的工作原理 2021-01-01
- 一起使用 MPI 和 OpenCV 时出现分段错误 2022-01-01