Re: Pointer casting related question

On 7 August 2017 at 14:59, Chris Vine <vine35792468 gmail com> wrote:
A static cast will carry out pointer adjustment when traversing an
inheritance graph to account for multiple inheritance or a vtable.  It
is reinterpret cast which will not.

Indeed, thanks. I was only half-thinking at the time, and decided to err on the side of caution. :)

I am one of the seemingly unpopular people who think it's perfectly fine to static_cast if you know the conversion will always be valid - i.e. so long as the program is not already in the process of crashing or failing asserts, which obviously should not be treated as reachable in released builds.

I often wish the stdlib had an asserted_static_cast<T>(arg) as this is a pattern I use often:
    assert( dynamic_cast<T>(arg) );
    auto& ref = *static_cast<T>(arg);
    // Use ref

and I've not yet gotten around to creating my own little header to reduce the verbosity of this.

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]