typeid

check if X is derived of Y via typeid

i need to convert pointers to long (SendMessage()) and i want to safely check if the variable is correct on the otherside. So i was thinking of doing dynamic_cast but that wont work on classes that are not virtual. Then i thought of doing typeid but that will work until i pass a derived var as its base. Is there any way to check if the ...

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

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...

typeid() returns extra characters in g++

class foo { public: void say_type_name() { std::cout << typeid(this).name() << std::endl; } }; int main() { foo f;; f.say_type_name(); } Above code prints P3foo on my ubuntu machine with g++. I am not getting why it is printing P3foo instead of just foo. If I change the code like std::cout << typeid(*this).name() <...

Test whether a class is polymorphic

Hello Group, We have a sub-project 'commonUtils' that has many generic code-snippets used across the parent project. One such interesting stuff i saw was :- /********************************************************************* If T is polymorphic, the compiler is required to evaluate the typeid stuff at runtime, and answer will be tr...

How to typeof in C++

How to simulate C# typeof-command behavior in C++? C# example: public static PluginNodeList GetPlugins (Type type) { ... } Call: PluginManager.GetPlugins (typeof(IPlugin)) How to implement this using C++? Maybe QT or Boost libraries provide a solution? What about the case if you want to implement .GetPlugins(...) in a way that i...

When can typeid return different type_info instances for same type?

Andrei Alexandrescu writes in Modern C++ Design: The objects returned by typeid have static storage, so you don't have to worry about lifetime issues. Andrei continues: The standard does not guarantee that each invocation of, say, typeid(int) returns a reference to the same type_info object. Even though the standard...

Forward declaration and typeid

I would like to check the type of a superclass A against the type of a subclass B (with a method inside the superclass A, so that B will inherit it). Here's what I thought did the trick (that is, the use of forward declaration): #include <iostream> #include <typeinfo> using namespace std; class B; class A { public: int i_; ...

typeid and typeof in C++

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 #inclu...

Get type of variable

If I understand correctly, typeid can determine the actual type in polymorphism, while typeof cannot. Is it 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? Is there any way to both get the actual type for polymorphism a...

What's the lifetime of memory pointed to typeinfo::name()?

In C++ I can use typeid operator to retrieve the name of any polymorphic class: const char* name = typeid( CMyClass ).name(); How long will the string pointed to by the returned const char* pointer available to my program? ...

typeid , dynamic casting (upcast) and templates

Hello, I have few questions regarding dynamic casting , typeid() and templates 1) How come typeid does not require RTTI ? 2) dynamic_cast on polymorphic type: When I do downcast (Base to Derive) with RTTI - compilation passes When RTTI is off - I get a warning (warning C4541: 'dynamic_cast' used on polymorphic type 'CBase' with /...

Is typeid of type name always evaluated at compile time in c++ ?

I wanted to check that typeid is evaluated at compile time when used with a type name (ie typeid(int), typeid(std::string)...). To do so, I repeated in a loop the comparison of two typeid calls, and compiled it with optimizations enabled, in order to see if the compiler simplified the loop (by looking at the execution time which is 1us ...

typeid result across different dll's

Hello, 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(...

Why switch expressions of type 'System::Guid' are illegal?

void Foo(Type^ type) { System::Guid id = type->GUID; switch (id) { case System::Byte::typeid->GUID: ... break; ... } Obviously case expressions are not constant. But I'd like to know why GUIDs cannot be known at compile time? (silly question I guess). At the end of the day it looks you have to use imbricated if the...

Pointer to the start of an object (C++)

Hello, I need a way to get a pointer to the start of an object in C++. This object is used inside a template so it can be any type (polymorphic or not) and could potentially be an object that uses multiple inheritance. I found this article which describes a way to do it (see the section called "Dynamic Casts") using typeid and a dynami...

typeid operator in C++

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 = str...