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

int f() {


Recommended Answers

All 3 Replies

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

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.

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.