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.
For example, assume I’ve a Result templated class:
template<type T>
class Result {
T _result_value;
T& operator=( T that );
~Result( );
}
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 */ >
so I could use it:
HFILE MyOpenFile( ... ) {
RESULT result;
}
…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.
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 todecltype.Not that you need it, with
auto.