Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'typeid' versus 'typeof' in C++

Tags:

c++

typeof

typeid

I am wondering what the difference is between typeid and typeof in C++. Here's what I know:

  • typeid is mentioned in the documentation for type_info which is defined in the C++ header file typeinfo.

  • typeof is defined in the GCC extension for C and in the C++ Boost library.

Also, here is test code test that I've created where I've discovered that typeid does not return what I expected. Why?

main.cpp

#include <iostream>   #include <typeinfo>  //for 'typeid' to work    class Person {       public:     // ... Person members ...       virtual ~Person() {}   };    class Employee : public Person {       // ... Employee members ...   };    int main () {       Person person;       Employee employee;       Person *ptr = &employee;       int t = 3;        std::cout << typeid(t).name() << std::endl;       std::cout << typeid(person).name() << std::endl;   // Person (statically known at compile-time)       std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)       std::cout << typeid(ptr).name() << std::endl;      // Person * (statically known at compile-time)       std::cout << typeid(*ptr).name() << std::endl;     // Employee (looked up dynamically at run-time                                                          // because it is the dereference of a pointer                                                        // to a polymorphic class)    }   

output:

bash-3.2$ g++ -Wall main.cpp -o main   bash-3.2$ ./main    i   6Person   8Employee   P6Person   8Employee 
like image 260
Tim Avatar asked Dec 31 '09 17:12

Tim


2 Answers

C++ language has no such thing as typeof. You must be looking at some compiler-specific extension. If you are talking about GCC's typeof, then a similar feature is present in C++11 through the keyword decltype. Again, C++ has no such typeof keyword.

typeid is a C++ language operator which returns type identification information at run time. It basically returns a type_info object, which is equality-comparable with other type_info objects.

Note, that the only defined property of the returned type_info object has is its being equality- and non-equality-comparable, i.e. type_info objects describing different types shall compare non-equal, while type_info objects describing the same type have to compare equal. Everything else is implementation-defined. Methods that return various "names" are not guaranteed to return anything human-readable, and even not guaranteed to return anything at all.

Note also, that the above probably implies (although the standard doesn't seem to mention it explicitly) that consecutive applications of typeid to the same type might return different type_info objects (which, of course, still have to compare equal).

like image 114
AnT Avatar answered Sep 25 '22 15:09

AnT


The primary difference between the two is the following

  • typeof is a compile time construct and returns the type as defined at compile time
  • typeid is a runtime construct and hence gives information about the runtime type of the value.

typeof Reference: http://www.delorie.com/gnu/docs/gcc/gcc_36.html

typeid Reference: https://en.wikipedia.org/wiki/Typeid

like image 34
JaredPar Avatar answered Sep 21 '22 15:09

JaredPar