I am not sure about other versions, but, in VS 2008 even with RTTI disabled I can query the information by calling typeid(T).name()
on the type. It works with all types, internal and user created.
Our project has RTTI disabled but I was surprised to find that this still worked. Is this a Visual Studio bug or is the /GR-
flag not enough to disable it? Does this fall into the realm of undefined behavior
? If it is not a bug, how do I really turn it off?
Is it possible that you used typeid
on an expression whose type is known at compile time? If so you would bypass the "run-time" aspect of RTTI altogether. From the Microsoft documentation on typeid
:
If the expression is neither a pointer nor a reference to a base class of the object, the result is a type_info reference representing the static type of the expression. The static type of an expression refers to the type of an expression as it is known at compile time. Execution semantics are ignored when evaluating the static type of an expression.
First of all, using typeid
(or dynamic_cast
) when RTTI is disabled is definitely going to lead to undefined behavior. My advice would be to not do that.
In any case, I don't believe it's the case that you need to do anything other than /GR-
to disable RTTI in Microsoft's compiler. It's perhaps a bug that it doesn't give a warning or error when doing so. It does give a warning for dynamic_cast
:
1>d:\src\MyClass.cpp (82) : warning C4541: 'dynamic_cast' used on polymorphic type 'MyClass' with /GR-; unpredictable behavior may result
However, it gives no such warning for typeid
. This hasn't changed in the VS2010 compiler either.
Side note: gcc gives a compile error if you try to use typeid
with -fno-rtti
, while clang does not (even with -Wall
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With