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

问题描述

因为我有一个库,它以如下形式暴露了一堆函数:

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();

编译器现在做什么?

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

09-05 20:12
查看更多