Storing std::type_info

Discussion in 'C++' started by =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 11, 2007.

  1. On 2007-08-11 16:02, Daniel Kraft wrote:
    > 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"; }
    }

    --
    Erik Wikström
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 11, 2007
    #1
    1. Advertising

  2. * Erik Wikström:
    > On 2007-08-11 16:02, Daniel Kraft wrote:
    >> 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.


    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...

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 11, 2007
    #2
    1. Advertising

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

    Daniel Kraft Guest

    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.

    --
    Got two Dear-Daniel-Instant Messages
    by MSN, associate ICQ with stress--so
    please use good, old E-MAIL!
    Daniel Kraft, Aug 11, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. sks_cpp

    type_info, vtable

    sks_cpp, Aug 19, 2003, in forum: C++
    Replies:
    54
    Views:
    2,244
    Ron Natalie
    Aug 27, 2003
  2. skscpp
    Replies:
    5
    Views:
    562
    EventHelix.com
    Nov 14, 2003
  3. Bobo

    virtual ~type_info();

    Bobo, Jan 20, 2004, in forum: C++
    Replies:
    3
    Views:
    376
  4. Rahul

    type_info

    Rahul, Dec 8, 2007, in forum: C++
    Replies:
    9
    Views:
    681
    James Kanze
    Dec 9, 2007
  5. sumsin

    What is type_info object?

    sumsin, Jun 11, 2008, in forum: C++
    Replies:
    1
    Views:
    410
    James Kanze
    Jun 12, 2008
Loading...

Share This Page