Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use typeid to store a type_info object

Tags:

c++

I am trying to create and store a type_info object:

#include <typeinfo>
int i;
type_info x = typeid(i);

And an error message is generated. Is there any way to store a type_info object?

The history behind this is that I am trying to generate test cases for various C++ integer types; doing arithmetic on them and determining whether intermediate results are promoted to the next largest integer type or truncated. That is:

unsigned char x = 257;
unsigned char y = 257;
// is (x + y) == 514 or 256?

And decided to do a type check against a static data structure, for example:

int x = <value>;
int y = <value>;
static type_info def = { typeid(bool)
                       , typeid(char),  typeid(unsigned char)
                       , typeid(short), typeid(unsigned short)
                       , typeid(long),  typeid(unsigned long)
                       };
type_info obj = typeid(x + y);
for(int i = 0; i < sizeof(def)/sizeof(def[0]); i++) if (obj == def[i]); break;

Anyhow, it can't be done without being able to store the type_info structure and I would still like to find out about integer promotions.

Can you create a type_info object? The gcc 4.5.3 implementation has assignment as private.

Is there a resource telling when integer promotions are performed?

thanks

like image 356
skidmarks Avatar asked Oct 22 '25 21:10

skidmarks


1 Answers

typeid() returns a const type_info & and type_info has a private copy constructor, so you can't build the array you describe. I can't find anything to say whether they are persistent, but you could try type_info *a[] = { &typeid(int), ... }

like image 125
Ben Jackson Avatar answered Oct 24 '25 10:10

Ben Jackson