Re: VC2008 Bug: optimising static_cast to a virtual base gives C1001 internal compile error
jesse.pepper@gmail.com wrote:
I just created a new solution with a single file, test.cpp, whose
contents are:
struct B {};
struct A : public virtual B {};
int main()
{
B* b = reinterpret_cast<A*>(0);
return 0;
}
now when I right-click on the .cpp and chose "compile" the bloody
thing does compile successfully... note for Ulrich's sake I used
reinterpret_cast (which also fails in the original context).
Sorry, but that cast is wrong, too. Just use a static_cast, or in the case
of a null pointer, don't use a cast at all. The only real use of
reinterpret_cast is when I'm converting a pointer to an integer and back,
e.g. as user data to attach to a win32 control.
You mentioned that you are checking if the argument to a macro is derived
from a certain baseclass. I'd do it this way:
inline void can_convert_to_base( base const& b) {/*empty*/}
#define MAKRO(x) do { can_convert_to_base(x); ... } while(false)
What is the difference? Firstly, there is no cast involved. Only if the
conversion can be performed implicitly it will actually be done, otherwise
you get a compile error for the function call. Note that this doesn't
detect errors when 'x' has an overloaded conversion operator to 'base
const&', but I guess you could live with that.
Otherwise, you could try to static_cast it back to the type of 'x',
unless 'base' is a virtual baseclass where you need to use 'dynamic_cast'.
I believe Boost also has a metaprogramming class that checks if some class
is (publicly) derived from another which you might take a look at.
You could also use a temporary reference like
base const& tmp = x;
which would achieve the same. In neither case would I use a cast unless
absolutely necessary.
However, building the solution (as opposed to the individual .cpp)
crashes with a different C1001 error at link time!!!
Just for your understanding, the compile/link separation is blurred with
modern compilers. I guess you somewhere have a /LTCG in the linker settings
and a 'whole program optimisation' in the compiler settings. This means
that the compiler will only perform syntax checks and create the calltree
and type information, while the linker combines all those files and then
perform the optimisations on them.
The plot thickens
;)
Uli
--
C++ FAQ: http://parashift.com/c++-faq-lite
Sator Laser GmbH
Gesch??ftsf??hrer: Michael W??hrmann, Amtsgericht Hamburg HR B62 932