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
        assert( static_cast<const Source*>(dynamic_cast<Target>(x)) == x );
// detect logic error
        assert( dynamic_cast<Target>(x) == x ); // detect logic error
        return static_cast<Target>(x);
The problem is not present in VS 2005.


Boost list run by bdawes at, gregod at, cpdaniel at, john at