C ++ How to get the type of an object?

how can i get the typeidfollowing T& object:; where T: template<class T>. I work in C ++ code. I checked:

const std::type_info& ObjT= typeid(object);
std::cout<<"******the objT is: "<<&ObjT<<std::endl;

But he is falling. Why?

+3
source share
3 answers

did you include:

#include <typeinfo>

and maybe you should send

std::cout << typeid(object).name() << std::endl;

And maybe there is a problem in what you do && at the facility.

look at an example here

+3
source

If you want to get a human-accessible name, use the method name():

std::cout<<"******the objT is: "<<ObjT.name()<<std::endl;
+4
source

Is that what you do?

#include <typeinfo>
#include <iostream>

template <typename T> 
struct S {
};

int main() {
    S <int> a;
    S <int> & object = a;
    const std::type_info & ObjT = typeid( object );
    std::cout << "******the objT is: "<< ObjT.name() <<  std::endl;
}

This works for me with GCC 4.5.1.

0
source

All Articles