typeid

dynamic_cast issues: typeid object is not equal, but name is equal

被刻印的时光 ゝ 提交于 2021-02-16 07:49:11
问题 I found that dynamic_cast didn't work in a situation where I expected it to, and looking at the typeid of the objects at runtime has made the situation even less clear. I just want a cast from base to derived, and I can't figure out why it's not working. I have a class structure something like this: class BoundaryCondition { public: virtual void DoSomething() = 0; virtual ~BoundaryCondition() { /* * */ } } class ReflectingBc : BoundaryCondition { public: virtual void DoSomething(); } class

typeid operator in C++

心不动则不痛 提交于 2021-02-08 19:49:15
问题 I have the following code int main() { cout << "Please enter your name..." << endl; cin >> name; cout << "Data type = " << typeid(name).name() << endl; cin.get(); return 0; } According to the various textbooks and pieces of documentation I've read about the typeid operator, I should expect to read "Data type = string" as the output. Instead, I get the following class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > Does anyone have any idea where I'm going

Is it possible to get a char* name from a template type in C++

流过昼夜 提交于 2021-02-04 11:29:05
问题 I want to get the string name (const char*) of a template type. Unfortunately I don't have access to RTTI. template< typename T > struct SomeClass { const char* GetClassName() const { return /* magic goes here */; } } So SomeClass<int> sc; sc.GetClassName(); // returns "int" Is this possible? I can't find a way and am about to give up. Thanks for the help. 回答1: No and it will not work reliable with typeid either. It will give you some internal string that depends on the compiler

Difference between type alias and using-declaration

时间秒杀一切 提交于 2021-01-27 07:19:39
问题 Is there any difference between using a type alias and alias template on the one hand and using-declaration on the other hand defined as follows: Given a class definition in a namespace scope: namespace ns1 { template< typename T > struct A {}; } And given another namespace, which introduces the A symbol by two different ways. First of them is type alias ( alias template ): namespace ns2 { template< typename T > using A = ns1::A< T >; // match type-id } Second one is using-declaration :

Why does 'typeid(x) == typeid(y)' evaluate to true, where 'x' and 'y' are id-expression of type T and T& respectively?

|▌冷眼眸甩不掉的悲伤 提交于 2020-06-25 18:03:33
问题 I am reading the C++11 draft standard and the section on [expr.typeid] mentions the following (emphasis mine): [...] When typeid is applied to an expression other than a glvalue of a polymorphic class type, the result refers to a std::type_info object representing the static type of the expression. Lvalue-to-rvalue (4.1), array-topointer (4.2), and function-to-pointer (4.3) conversions are not applied to the expression. If the type of the expression is a class type, the class shall be

Can type_info pointers be used to distingush types in C++?

好久不见. 提交于 2020-01-15 23:13:24
问题 I have a set of polymorphic C++ classes and they are all instantiated by the same module (Windows DLL). Now having two pointers to such classes and having called typeid : SomeCommonBase* first = ...; //valid pointer SomeCommonBase* second = ...; //valid pointer const type_info& firstInfo = typeid( first ); const type_info& secondInfo = typeid( second ); can I compare retrieved type_info addresses if( &firstInfo == &secondInfo ) { //objects are of the same class } else { //objects are of

typeid result across different dll's

谁说我不能喝 提交于 2020-01-14 19:34:13
问题 I have two dlls which both declare a templated type, let's call A. If the declaration of A is sufficiently intricate, it happens that the result of typeid(A).name() is different when called in functions in two different dll's. example: DLL1: struct MyType: public A< TEMPLATE_LIST_OF_A >{} void f(){ std::string name1 = typeid(A).name(); } DLL2: struct MyType: public A< TEMPLATE_LIST_OF_A >{} void f(){ std::string name2 = typeid(A).name(); } for example name1 could be something like: "???MyType

C++: type_info to distinguish types

醉酒当歌 提交于 2020-01-11 16:35:28
问题 I know that compilers have much freedom in implementing std::type_info functions' behavior. I'm thinking about using it to compare object types, so I'd like to be sure that: std::type_info::name must return two different strings for two different types. std::type_info::before must say that Type1 is before Type2 exclusive-or Type2 is before Type1 . // like this: typeid(T1).before( typeid(T2) ) != typeid(T2).before( typeid(T1) ) Two different specialization of the same template class are

When is using 'typeid' the best solution?

心已入冬 提交于 2020-01-10 17:27:21
问题 There are many reasons not to use typeid . Other than for using members of type_info (implementation defined behavior), it is usually (always?) possible to provide similar functionality using other C++ language features, eg: overloading, virtual functions etc. So, excluding usage that relies on the implementation defined behavior, does anybody have a real world example where typeid is the best solution? 回答1: boost::any uses typeid to implement any_cast . template<typename T> any_cast(const