Storing std::type_info

  • Thread starter =?ISO-8859-1?Q?Erik_Wikstr=F6m?=
  • Start date
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

Hi all,

I tried to store a std::type_info value in my code so I didn't have to
call typeid more than once when checking if a polymorphic object is of
one of certain types, like this:

#include <typeinfo>

class A
{
public:
virtual int foo() { return 42; }
};

class B : public A
{};

class C : public A
{};

void foo(A& obj)
{
std::type_info type(typeid(obj));

if(type==typeid(B)) { ... }
if(type==typeid(C)) { ... }
}

I found, however, that the copy-constructor of type_info was private; is
it save to store the value by reference:

const std::type_info& type(typeid(obj)); ?

This compiles fine, but I'm not sure whether this stores the reference
to a temporary object which is no longer valid when the comparison happens.

Is is ok to do this with references? Or is there any other way to store
the type_info or do I have to insert typeid(obj) everywhere in the ifs?

Yes, a const reference can be bound to a temporary.

Or you can use & to get the address of the type_info object (typeid
returns a reference) but in that case you should use type_info's ==
operator and not compare addresses of type_info objects, i.e.
void foo(A& obj)
{
const std::type_info* type = &typeid(obj);

if(*type==typeid(B)) { std::cout << "B\n"; }
if(*type==typeid(C)) { std::cout << "C\n"; }
}
 
A

Alf P. Steinbach

* Erik Wikström:
Yes, a const reference can be bound to a temporary.

Well, technically that doesn't happen here. The reference is bound to
the reference returned by typeid. Otherwise with the current standard
an accessible copy constructor would be required.

Or you can use & to get the address of the type_info object (typeid
returns a reference) but in that case you should use type_info's ==
operator and not compare addresses of type_info objects, i.e.
void foo(A& obj)
{
const std::type_info* type = &typeid(obj);

if(*type==typeid(B)) { std::cout << "B\n"; }
if(*type==typeid(C)) { std::cout << "C\n"; }
}

I think better to set the compiler more free to do its job without any
deep analysis, i.e., using the reference, or just typeid directly.
Which might actually be more efficient. However, this kind of thing
falls squarely into the Evil Premature Optimization category...
 
D

Daniel Kraft

Hi all,

I tried to store a std::type_info value in my code so I didn't have to
call typeid more than once when checking if a polymorphic object is of
one of certain types, like this:

#include <typeinfo>

class A
{
public:
virtual int foo() { return 42; }
};

class B : public A
{};

class C : public A
{};

void foo(A& obj)
{
std::type_info type(typeid(obj));

if(type==typeid(B)) { ... }
if(type==typeid(C)) { ... }
}

I found, however, that the copy-constructor of type_info was private; is
it save to store the value by reference:

const std::type_info& type(typeid(obj)); ?

This compiles fine, but I'm not sure whether this stores the reference
to a temporary object which is no longer valid when the comparison happens.

Is is ok to do this with references? Or is there any other way to store
the type_info or do I have to insert typeid(obj) everywhere in the ifs?

Yours,
Daniel

PS: I know that RTTI should be uses as rarly as possible, please don't
tell me I should use something else. In my real situation, I believe
RTTI similar to this is what I want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top