有什么办法获得“当前函数”的返回类型。 [英] Is there any way to get the return type of the "current function"
问题描述
我想写一个模板,它将获得作为参数的函数的返回类型,其中它被实例化。
I would like to write a template that would get as a parameter the return type of the function in which it is being instantiated.
例如, ve 结果
模板类:
For example, assume I've a Result
templated class:
template<type T>
class Result {
T _result_value;
T& operator=( T that );
~Result( );
}
这个类会有几个特殊化。在析构函数中,我想记录返回类型,在 operator =
赋值中,我想检查并声明错误值。
There would be several specializations for this class. In the destructor I would like to log the return type, and within the operator=
assignment I would like to check and assert for error values.
理想情况下,我想能够有这样的定义:
Ideally, I would like to be able to have such a define:
#define RESULT Result< /* decltype magic for type of current function */ >
所以我可以使用它:
HFILE MyOpenFile( ... ) {
RESULT result;
}
...将被推导到 HFILE>
。这是一个简单的例子:写 RESULT
而不是结果< HFILE>
不是一个大问题,其他情况下,当前函数的返回类型不容易获得。
...which will be deduced to Result<HFILE>
. This is a simplified example: writing RESULT
instead of Result<HFILE>
isn't a big deal, but there are other scenarios where the return type of the current function isn't easily obtained.
推荐答案
否。 C ++中没有什么是指当前函数。最接近的是 __ func __
但是这是一个字符串字面量。因此,没有什么可以传递给 decltype
。
No. There's nothing in C++ which refers to the "current function". The closest is __func__
but that's a string literal. Hence, there's nothing to pass to decltype
.
不是你需要的, auto
。
这篇关于有什么办法获得“当前函数”的返回类型。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!