tags:

views:

1148

answers:

4

I just wonder what's the difference between typeid and typeof in C++?

typeid is defined in standard C++ Library Header File typeinfo.

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


update:

Thank you!

my following test code for typeid does not output the correct type name. what's wrong?

 //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
+5  A: 

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 Refenence: http://www.delorie.com/gnu/docs/gcc/gcc_36.html

typeid Rfeerence: http://en.wikipedia.org/wiki/Typeid

JaredPar
Thank you, JaredPar! I have some new questions in the updated post after reading your replies. Such as if it is also true that their returns are used for different purposes: the return of typeof is used as type keyword that can define variable, but the return of typeid cannot?
Tim
+5  A: 

typeid can operate at runtime, and return an object describing the run time type of the object, which must be a pointer to an object of a class with virtual methods in order for RTTI (run-time type information) to be stored in the class. It can also give the compile time type of an expression or a type name, if not given a pointer to a class with run-time type information.

typeof is a GNU extension, and gives you the type of any expression at compile time. This can be useful, for instance, in declaring temporary variables in macros that may be used on multiple types. In C++, you would usually use templates instead.

Brian Campbell
As far as I know, `typeid` will accept any expression, not just those that evaluate to objects with virtual methods. Furthermore, `typeid` will accept a type *name*, not just an expression. You can say `typeid(5)` or `typeid(std::string)` if you want.
Rob Kennedy
I've clarified my answer to make that clear; `typeid` *can* return run-time type information if available, but will provide compile time type information for anything else.
Brian Campbell
Thank you, Brian and Rob! I have some new questions in the updated post after reading your replies.
Tim
+2  A: 

Answering the additional question:

my following test code for typeid does not output the correct type name. what's wrong?

There isn't anything wrong. What you see is the string representation of the type name. The standard C++ doesn't force compilers to emit the exact name of the class, it is just up to the implementer(compiler vendor) to decide what is suitable. In short, the names are up to the compiler.


These are two different tools. typeof returns the type of an expression, but it is not standard. In C++0x there is something called decltype which does the same job AFAIK.

decltype(0xdeedbeef) number = 0; // number is of type int!
decltype(someArray[0]) element = someArray[0];

Whereas typeid is used with polymorphic types. For example, lets say that cat derives animal:

animal* a = new cat; // animal has to have at least one virtual function
...
if( typeid(*a) == typeid(cat) )
{
    // the object is of type cat! but the pointer is base pointer.
}
AraK
Thank you, Arak! I just updated the post with some new questions. Please take a look if possible.
Tim
+2  A: 

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 planned to be introduced into C++ in the future through keywords decltype and auto. Again, currently C++ has no such feature, neither through typeof nor through any other 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 consequitive applications of typeid to the same type might return different type_info objects (which, of course, still have to compare equal).

AndreyT
Thank you, AndreyT! I just updated the post with some new questions. Please take a look if possible.
Tim