tags:

views:

151

answers:

6

Sometimes I need to learn the type of an expression while programming in C or C++. Sometimes there's a good IDE or existent documentation to help me, but sometimes not. I often feel such a construct could be useful:

void (*myFunc)(int);
printf("%s", nameoftype(myFunc)); //"void (*)(int)"
int i, unsigned int u;
printf("%s", nameoftype(i+u));    //"unsigned int"

This is especially true for C++; think accessors of const objects - do they return a const reference or a copy? Think dynamic casts and templated classes.

How can I do this? (i.e. learn the type of an expression)

I use GCC but as far as I know, it does not have such an extension. So I guess I'm curious as to how people solve this problem. (Both compile-time and runtime solutions welcome.)

+2  A: 

C++ has a typeid operator;

typeid(expression).name()

would return an implementation-defined name of the type of the expression. Alas, it is usually not human-readable.

aib
If youare using g++ it is the mangeled name. You can then use the command line tool 'c++filt' to de-mangle it.
Martin York
Ah, I've never used c++filt. I'll look into it, thanks.
aib
+2  A: 

Sometimes I just do:

int ***a = expression;

and look for the "<expression type> cannot be assigned to pointer-to^3 int" error. This seems to be the most portable workaround.

aib
Some older C compilers will actually allow that though. :-(
T.E.D.
I create a class 'FailAssign' then try to assign an expression to a instance of the class. That way no auto conversions kick in ( even buggy ones).
Martin York
Hmm, good idea. Though my way lets you declare a triple pointer! How often do you get to do that? :)
aib
A: 

Variables have types in C++, just jump to the declaration in you IDE.

soulmerge
Is there any IDE smart enough to infer the type of a more complex expression [than a simple variable]? Any that can discern between the const and non-const versions of functions in C++?
aib
aib, according to http://www.kdevelop.org/mediawiki/index.php/KDevelop_4/KDev3_KDev4_comparison_table, kdevelop4 can do it: "Automatic creation of functions/variables based on context type deduction". It's still in development but i heard some people use it already. Worth a try i think.
Johannes Schaub - litb
+1  A: 

Try Boost.Typeof to see if it fits.

Anton Gogolev
It seems like a good solution for type inference, but I did not see a macro that evaluates to, or a function that returns a string containing the type name. One could be added, though, seeing as how it uses central REGISTER_() macros.
aib
+1  A: 

gcc has typeof() at compile time. It works like sizeof().

http://gcc.gnu.org/onlinedocs/gcc/Typeof.html has more information.

Eyal
+1  A: 

What are you looking for? Automatic type inference or looking for the type so you can declare a variable correctly manually? (your own answers look like you want to have the second one). In this case, consider using Geordi:

<litb> make type pointer to function taking pointer to array of 10 int returning void
<geordi> void (*)(int (*)[10])

<litb> geordi: { int a = -1; unsigned int b = 0; cout << ETYPE(a + b), ETYPE_DESC(a + b), (a + b); }
<geordi> rvalue unsigned int, rvalue unsigned integer, 4294967295

<litb> geordi: << TYPE_DESC(void (*)(int (*)[10]))
<geordi> pointer to a function taking a pointer to an array of 10 integers and returning nothing

Automatic type inference is not currently possible without helper libraries like boost.typeof, which will use compiler extensions like __typeof__ for GCC. Next C++ will get auto (with different semantics than current auto) and will be able to do that, together with decltype to get the type of an expression.

If you can live with getting out of local context, you can always create a function template like this:

template<typename T> void f(T t) { /* ... */ }
int main() { int a = -1; unsigned int b = 0; f(a + b); }
Johannes Schaub - litb
Sorry, I thought it was obvious I wasn't looking for type inference. Though I'm looking forward to seeing some new sphagetti C++0x code with automatic variables all around :) -- Wow, I'd forgotten Geordi could do type descriptions. I'll have a look at the implementation. Also thanks for the KDevelop link. It might make an alternative to Eclipse.
aib