Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find the return type of a function?

Tags:

c++

So __FUNCTION__ tells us the name of the current function. Is there a similar macro for return type of the function?

like image 968
Bruce Avatar asked Dec 01 '25 14:12

Bruce


2 Answers

Since you already use a Visual-C++ specific __FUNCTION__ macro you could look into __FUNCSIG__ macro - it expands into the full signature which you could then parse and extract the return type. There's no macro that would do that on itself in Visual C++.

like image 196
sharptooth Avatar answered Dec 03 '25 03:12

sharptooth


If you don't need it at preprocessing time:
In C++0x, you can do use decltype(myFunc(dummyparams)), which stands for the return type of myFunc (compile-time).
If you now want a string out of that, enable RTTI (run-time type information) and use typeid().name() (run-time):

#include <typeinfo>

double myFunc(){
  // ...
  return 13.37;
}

int main(){
  typedef decltype(myFunc()) myFunc_ret;
  char const* myFunc_ret_str = typeid(myFunc_ret).name();
}

Note, however, that the myFunc_ret_str isn't always a clean name of the return type - it depends on how your compiler implements typeid.

like image 33
Xeo Avatar answered Dec 03 '25 03:12

Xeo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!