Boost logo

Boost :

From: Mat Marcus (mat-lists_at_[hidden])
Date: 2005-12-19 19:10:06


Visual studio 2003 seems to be buggy in a way that can break
boost::polymorphic_downcast. Unless I am missing something, it's a rather
scary silent failure:

###

# include <cassert>

class left {}; class right {};

class derived : public left, public right {};

int _tmain(int argc, _TCHAR* argv[])
{
    derived x;
    const derived *xp=&x;
    right *yp=&x;
// assert(const_cast<derived*>(xp)==yp); no bug
    assert(xp==yp); //bug: this fires
    return 0;
}

###

This bug causes the assert in polymorphic_downcast to fire when it
shouldn't. Here is one possible patched version:

    template <class Target, class Source>
    inline Target polymorphic_downcast(Source* x
BOOST_EXPLICIT_DEFAULT_TARGET)
    {
#if BOOST_WORKAROUND(BOOST_MSVC, <= 1310)
        assert( static_cast<const Source*>(dynamic_cast<Target>(x)) == x );
// detect logic error
#else
        assert( dynamic_cast<Target>(x) == x ); // detect logic error
#endif
        return static_cast<Target>(x);
    }
###
The problem is not present in VS 2005.

Thoughts?
Mat


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk