Is typeid is still in use ?

This is a discussion on Is typeid is still in use ? within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <typeinfo> using namespace std; class point { int x, y ; public: point(int Ix, int Iy) ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    Is typeid is still in use ?

    Code:
    #include <iostream>
    #include <typeinfo>
    
    using namespace std;
    
    class point 
       {
       int  x, y ;
    public:
       point(int Ix, int Iy) : x(Ix), y(Iy)
          {
          }
       void show(void)
          {
          cout << "x = " << x << " y = " << y << endl;
          }
    };
       
    
    int main(void)
    {
    point  p1(3,3);
    point  p2(99, 3922);
    
    cout << "point type = " << typeid(p1).name() << endl;
    cout << "point type = " << (typeid(p2)).name() << endl;
    cout << "int type = " << (typeid(int)).name() << endl;
    cout << "int type = " << typeid(int).name() << endl;
    cout << "unsigned int type = " << typeid(unsigned int).name() << endl;
    
    
    cout << "Press to continue ...." ;
    cin.get();
    return EXIT_SUCCESS;
    }
    The output :
    point type = 5point
    point type = 5point
    int type = i
    int type = i
    unsigned int type = j
    Press to continue ....


    why is it 5point and not point ?
    why the "unsigned int type = j" ? what is j ?

    I am using bloodshed 4.9.9.2
    Last edited by dude543; 01-30-2006 at 01:19 AM. Reason: add more data

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    AFAIK, the output of type_info::name() is not covered in the standard. It is down to the implmentation. So each compiler might have a different idea about what to return from that function

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    and every compiler behave diff

    Quote Originally Posted by Fordy
    type_info::name() is not covered in the standard.
    You mean to say that every compiler will return somthing out of the blue ?

    Is only type_info::name() is not covered in the standard.
    The rest of class type_info is covered by the standard ?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Here's the output I get when I run your program using VC++6.0:
    point type = class point
    point type = class point
    int type = int
    int type = int
    unsigned int type = unsigned int
    Press to continue ....

  5. #5
    chococoder
    Join Date
    Nov 2004
    Posts
    515
    It's not just compiler specific but platform specific...
    g++ on Redhat enterprise gives
    point type = 5point
    point type = 5point
    int type = i
    int type = i
    unsigned int type = j
    Press to continue ....
    which is different from bloodshed output you mention (bloodshed being g++ on win32).

    bcc 5.5 on win32 gives
    point type = point
    point type = point
    int type = int
    int type = int
    unsigned int type = unsigned int
    Press to continue ....

  6. #6
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786
    Quote Originally Posted by dude543
    You mean to say that every compiler will return somthing out of the blue ?

    Is only type_info::name() is not covered in the standard.
    The rest of class type_info is covered by the standard ?
    It is in the standard, but the standard says that how thew function works is implementation specific.

    A quick google suugests this

    18.5.1 Class type_info
    const char* name() const;
    Returns: an implementation-defined NTBS."
    (NTBS means null-terminated byte string)
    If that's correct, then technically, every call could return "Eat at Joe's" and still not fall foul of the standard. Not very helpful huh?

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    You are right

    The autoher of c++ "BJARNE STROUSTRUP"
    "The character repersentation of a class name is implementation
    defined. This C-style string resides in memory owned by the system, so the programmer should not attempt ot delete[] it."

    guess I will have to leave with that.


    PS : is bcc a good compiler ?

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, 06:44 PM
  2. typeid problem
    By mikahell in forum C++ Programming
    Replies: 13
    Last Post: 12-03-2006, 05: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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21