absl::down_cast

down_cast overloads

Synopses

Declared in <absl/base/casts.h>

Overload of down_cast for references. Use like this: absl::down_cast<T&>(foo). The code is slightly convoluted because we're still using the pointer form of dynamic cast. (The reference form throws an exception if it fails.)

template<
    typename To,
    typename From>
[[nodiscard]]
To
down_cast(From& f);
» more...

An "upcast", i.e. a conversion from a pointer to an object to a pointer to a base subobject, always succeeds if the base is unambiguous and accessible, and so it's fine to use implicit_cast.

template<
    typename To,
    typename From>
[[nodiscard]]
To
down_cast(From* f);
» more...

Return Value

  • f, downcast to To.
  • f, downcast to To, or null if f is null.

NOTE

The return value should not be discarded.

Parameters

NameDescription
fThe source reference to downcast.