adherent88
adherent88

Reputation: 53

How to use typeid to get the type name which defined use typedef

Here is my problem:

I defined a new type and used the new type to declare a variable:

typedef int new_type;
new_type value;

I need somehow get the new_type as a string back using typeid, but if I use:

typeid(value)

It will return int, instead of new_type.

Is there a way to get new_type as a string back?

Thanks!

Upvotes: 5

Views: 2443

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

typedef's aren't their own types. Think of it as an alias for the original type.

"Is there a way to get new_type as a string back?"

Since it isn't a real type of it's own, no.


Remember that the typeid() intrinsic has id in it's name, that means it should always return the same identifier for the (effectively) same types.

The only way I can think of to achieve what you want, is creating your own wrapper class/struct that behaves exactly like an int. But I doubt it's worth the efforts.

Upvotes: 5

Related Questions