0

Hello,

I need a way to get a function return type during preprocessing or compilation(meta...).
example:

int f() {
MACRO_OR_META_WHICH_RETRIEVES_FUNCTION_RETURN_TYPE

Thanks,
Leonid

3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by subkin13
0

Might I inquire why this would be necessary, as your return type cannot change?

Perhaps what you actually wanted was a template, I am not certain.

template <typename T>
T* foo( T t ) {return &T;}

In the code you have above, you may as well just use int, such as:

int f() {
  std::cout << sizeof(int) << std::endl; // outputs 4
  return sizeof(int);
}
0

There is information about a data type known as type_info here.

If you do something like this:

#include <iostream>
#include <typeinfo>

using namespace std;

int intFunc();

int main ()
{
 
  cout << "Type of intFunc is: " << typeid(intFunc).name();

  return 0;
}

You will get this:

Type of intFunc is: int __cdecl(void)

If you store this information to a string, you should be able to manipulate the string returned by type_info::name() to truncate the passing convention and parameter types just giving you the return type.

Edited by Fbody: n/a

0

This can be done with __FUNCTION__ macro as well. But I want only the 'int' and not as a string

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.