tags:

views:

306

answers:

3

We can get the type returned by function in gcc using the typeof operator as follows:

typeof(container.begin()) i;

Is it possible to do something similar for functions taking some arguments, but not giving them? E.g. when we have function:

MyType foo(int, char, bool, int);

I want to retrieve this "MyType" (probably using typeof operator) assuming I know only the name of function ("foo") and have no knowledge about arguments it takes. Is it possible?

+3  A: 

In C++ the return value's type is not part of the method signature. Even if there is a way to get the return type of a method, you would have to deal with the possibility of getting multiple methods back and not knowing which one went with the return type you want.

Kelly French
Though it's not part of the signature, it _is_ part of the type. You cannot assign an `int (*)( void )` to a `float (*)(void)`, for instance.
xtofl
+1  A: 

typeof is a non-standard extension, so don't use it if you want your code to be portable.

Its syntax is typeof(expression), so you need to give it an expression calling the function (whose type is therefore MyType), like this:

typeof(foo(int(),char(),bool(),int()))
Mike Seymour
+1. It might be worth pointing out that this expression is a so-called "unevaluated context" just like the expressions for sizeof are not evaluated.
sellibitze
A: 

C++0x is going to introduce a Type inference using the decltype and auto keywords.
In C++ you can use typeof (as suggested by Mike Seymour) or the SFINAE principle.

Oren S