How can Visual Studio use boost::function with /GR- while GCC can't with -fno-rtti?
I recently started porting an application to Android using the NDK and I encountered the following error:
boost/function/function_base.hpp:220: error: cannot use typeid with -fno-rtti.
Ordinarily, I wouldn't think twice about that error, but the visual studio pro开发者_Go百科ject file set /GR-
, which disables RTTI and has no problem using boost::function.
My theory on this is that because typeid is evaulated by the compilier when the static type can be determined, that must be the case for every usage of boost::function in the application. Visual Studio must be attempting to determine the static type first while GCC immediately throws the error on typeid before attempting to evaluate it.
Does that sound right? If not, what is going on?
I'm not booted into my Windows partition, but it was just as simple to look this up in MSDN.
As per this link on MSDN:
If the expression is dereferencing a pointer, and that pointer's value is zero, typeid throws a bad_typeid exception. If the pointer does not point to a valid object, a __non_rtti_object exception is thrown, indicating an attempt to analyze the RTTI that triggered a fault (like access violation), because the object is somehow invalid (bad pointer or the code wasn't compiled with /GR).
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.....
In other words, if the expression's type can't be inferred at compile time, and no /GR, then you're going to get an exception or compiler error. But the second paragraph kind of implies it can infer non-ambiguous types at compile time.
精彩评论