Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

retrieving type returned by function using "typeof" operator in gcc

Tags:

c++

gcc

typeof

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?

like image 567
peper0 Avatar asked Nov 09 '09 17:11

peper0


People also ask

What does typeof () return in C?

In other languages, such as C# or D and, to some degree, in C (as part of nonstandard extensions and proposed standard revisions), the typeof operator returns the static type of the operand. That is, it evaluates to the declared type at that instant in the program, irrespective of its original form.

What is __ typeof __ in C?

__typeof__() and __typeof() are compiler-specific extensions to the C language, because standard C does not include such an operator. Standard C requires compilers to prefix language extensions with a double-underscore (which is also why you should never do so for your own functions, variables, etc.)

What type does typeof return?

The typeof operator returns a string indicating the type of the unevaluated operand.


1 Answers

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()))
like image 80
Mike Seymour Avatar answered Nov 15 '22 07:11

Mike Seymour