Boost logo

Boost :

Subject: Re: [boost] generally replace *static_cast< T [const]* >(&x) -> static_cast< T [const]& >(x)?
From: Arno Schödl (aschoedl_at_[hidden])
Date: 2011-05-27 13:02:41

> I believe casting through pointers is more restrictive and predictable than
> casting through references (e.g., T& could be even implicitly convertible to
> U& without T* convertible to U*; even the case of converting base_type& to
> derived_type& could be using some converting constructor in derived_type),
> so I don't think this would be a wise change indiscriminately.

> It looks to me like in this iterator_facade case, though, a reference cast
> is probably fine...

Of course, I missed that. Arguably, in all such cases, boost::addressof should be used because the result of an overloaded operator& is just as unpredictable as the result of a conversion operator.

So something like:

template< class T, class S >
T const& inheritance_cast( S const& s ) {
        return *static_cast< T const* >( boost::addressof(s) );

template< class T, class S >
T & inheritance_cast( S & s ) {
        return *static_cast< T * >( boost::addressof(s) );


Dr. Arno Schödl | aschoedl_at_[hidden]
Technical Director
think-cell Software GmbH | Chausseestr. 8/E | 10115 Berlin | Germany | phone +49 30 666473-10 | US phone +1 800 891 8091
Amtsgericht Berlin-Charlottenburg, HRB 85229 | European Union VAT Id DE813474306
Directors: Dr. Markus Hannebauer, Dr. Arno Schoedl

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