为什么函数不能被返回类型重载?

2022-01-19 00:00:00 return types overloading c++

可能的重复:
按返回类型重载函数?
谜题:根据返回值重载一个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();

编译器现在做什么?

相关文章