Thread: About typeid..

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    32

    About typeid..

    Hi, could anyone help me how to using typeid correctly.
    I know it take class type or expression as argument.
    My question is if I have a class TypeA

    The normal usage is typeid(TypeA);
    If I have a string str and it'content is TypeA

    And I want to take compare if(typeid(str) == typeid(TypeA))
    I want the result is true.. Somehow the compiler only recognize its an string type
    I want it to compare its content and it's guaranteed the "content" is a class Type Name.

    How should I do?

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Don't. Use dynamic_cast. It's much more accurate and efficient.

    Code:
    template < typename Other, typename This >
    bool is_type( This const& value )
    {
    	return dynamic_cast< Other const* >( &value ) != 0;
    }
    
    // Example usage:
    
    struct A 
    {	};
    
    struct B
    {	};
    
    struct C : A, B
    {	};
    
    #include <cassert>
    
    using namespace std;
    
    int main( void )
    {
    	assert( is_type< A >( C( ) ) );
    	assert( is_type< B >( C( ) ) );
    }

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    Actually maybe I think it's not I want.
    What I have a list of string is only class Type "Name". Not real instance..
    I don't think it can work using dynamic_cast.

    I know typeid can directly evaluate class Name. Like typeid(Type)
    Question is I have a string str = "Type"
    and I want if (typeid(str) == typeid(Type)) to be true. Is that possible, or how should I modify it?
    The compiler always return typeid(str) the string type, but I want it to compare the content though

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    type_info has the name() method. However, how it decides to call the types is implementation-defined and you cannot rely on it being the same with different compilers.

    Code:
    #include <iostream>
    #include <typeinfo>
    #include <string>
    
    struct AType {};
    
    int main()
    {
        std::string str(typeid(AType).name());
        if (str == typeid(AType).name()) {
            std::cout << "It works and the name is " << str << '\n';
        }
    }
    Possible output:
    Code:
    It works and the name is 5AType
    --------
    Sebastiani: If you don't have virtual functions this will only work for upcasting. Not particularly useful, since it will either compile (and succeed), or not compile.
    Last edited by anon; 03-18-2010 at 10:15 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Feb 2010
    Posts
    32
    What I want is like following
    Code:
    #include <iostream>
    #include <typeinfo>
    #include <string>
    
    struct AType {};
    
    int main()
    {
    
        //std::string str(typeid(AType).name());
        std::string str = "AType";  // AType is a string I have
        if (typeid(str).name() == typeid(AType).name()) {
            std::cout << "It works and the name is " << str << '\n';
        }
        else
            std::cout << "There are not the same" << end;
    }
    The output is:
    Code:
    There are not the same
    And I want typeid(str) == typeid(AType)
    How should I do.?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The outside chance (that I am in no way actually suggesting you do) is to create a std::map from std::string to whatever type it is that typeid returns, and populate it somewhere with all the different types you (want to) know about.

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by anon View Post
    Sebastiani: If you don't have virtual functions this will only work for upcasting. Not particularly useful, since it will either compile (and succeed), or not compile.
    True. Of course, I always make it a policy to declare a virtual destructor for all of my classes, so that's never really been an issue for me. Anyway, maybe boost::is_polymorphic could be used in a workaround?

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    What are you trying to do again? Check if the object is of type TypeA?

    Code:
    if( typeid( object ) == typeid( TypeA ) )
        ...
    What's the problem again? Why are you introducing strings into this?

    EDIT: You realize that typeid(foo).name() is not guaranteed to hold anything you might expect? typeid(TypeA).name() could be "PopGoesTheWeasel" for all you know. All you know is that it's the SAME string for all objects of a given type.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #9
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Quote Originally Posted by ovid View Post
    What I want is like following
    Code:
    #include <iostream>
    #include <typeinfo>
    #include <string>
    
    struct AType {};
    
    int main()
    {
    
        //std::string str(typeid(AType).name());
        std::string str = "AType";  // AType is a string I have
        if (typeid(str).name() == typeid(AType).name()) {
            std::cout << "It works and the name is " << str << '\n';
        }
        else
            std::cout << "There are not the same" << end;
    }
    The output is:
    Code:
    There are not the same
    And I want typeid(str) == typeid(AType)
    How should I do.?
    Well, as Anon already pointed out, typeid::name() returns a compiler-specific string, so it'd be difficult to implement your system going that route.

    Perhaps you could elaborate a bit more on what you're trying to accomplish?

  10. #10
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I think you'll need a big if statement like:
    Code:
    if ( (str == "TypeA") && dynamic_cast<TypeA*>( &obj ) ) {
       cout << "str is a TypeA";
    }
    else if ( (str == "TypeB") && dynamic_cast<TypeB*>( &obj ) ) {
       cout << "str is a TypeB";
    }
    else if ( (str == "TypeC") && dynamic_cast<TypeC*>( &obj ) ) {
       cout << "str is a TypeC";
    }
    ...
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  11. #11
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    You could use something like this:
    Code:
    #define GET_TYPE_HELPER(TYPE) inline std::string getType(TYPE const){ return #TYPE;}
    GET_TYPE_HELPER(TypeA)
    GET_TYPE_HELPER(TypeB)
    GET_TYPE_HELPER(TypeC)
    GET_TYPE_HELPER(int)
    
    usage:
    signed eg1;
    getType(eg1); //"int"
    TypeA eg2(); 
    getType(eg2); //"TypeA"
    Last edited by King Mir; 03-18-2010 at 03:35 PM.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  12. #12
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    And I want to take compare if(typeid(str) == typeid(TypeA))
    I want the result is true.. Somehow the compiler only recognize its an string type
    I want it to compare its content and it's guaranteed the "content" is a class Type Name.
    Sounds like you want to compare objects based on their type. Bad mojo. There are several other approaches that do not require this. You can use unique identifiers which are a private data member in each class or for much simpler classes you could overload the equality operator to determine if the two objects are the same, etc, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to uuse 'typeid'
    By John_L in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2008, 07:44 PM
  2. typeid problem
    By mikahell in forum C++ Programming
    Replies: 13
    Last Post: 12-03-2006, 06:35 PM
  3. Debug function using typeid?
    By screemfead in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2006, 10:42 AM
  4. typeid and the type_info object
    By major_small in forum C++ Programming
    Replies: 1
    Last Post: 08-24-2004, 11:01 AM
  5. Typeid and Templates
    By Trauts in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2003, 08:55 PM