Search code examples
c++rttitypeid

typeid and type_info class


namespace std {
 class type_info
 {
 public:
  virtual ~type_info(); //type_info can serve as a base class
 // enable comparison
  bool operator==(const type_info& rhs ) const;
 // return !( *this == rhs)
  bool operator!=(const type_info& rhs ) const;
  bool before(const type_info& rhs ) const; // ordering
 //return a C-string containing the type's name
  const char* name() const;
 private:
  //objects of this type cannot be copied
     type_info(const type_info& rhs );
     type_info& operator=(const type_info& rhs);
 }; //type_info
}  

In the declaration of type_info class ,I can't find any data member .So what is constructed or destructed ?? Also typeid isn't declared in it.So how type_info object is accessed by it?
Is above representation incomplete? Please tell about the type of data member in type_info class


Solution

  • It looks like you are looking at the public interface of typeinfo from C++03. The standard doesn't restrict an implementation from adding members to a standard class (so long as there names come from those reserved to the implementation) to make things work.

    In the implementation that I am currently using std::typeinfo has a private member const char* __name which is used to implement the public member functions according to the requirements of the standard.

    ISO/IEC 14882:2011 17.5.2.3 Private members [objects.within.classes] / 1:

    Clauses 18 through 30 and Annex D do not specify the representation of classes, and intentionally omit specification of class members (9.2). An implementation may define static or non-static class members, or both, as needed to implement the semantics of the member functions specified in Clauses 18 through 30 and Annex D.

    Similar wording appears in C++03 17.3.2.3.