[#absl-down_cast-06]
= xref:absl.adoc[absl]::down&lowbar;cast
:relfileprefix: ../
:mrdocs:

An &quot;upcast&quot;, i&period;e&period; 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&apos;s fine to use implicit&lowbar;cast&period;

== Synopsis

Declared in `&lt;absl&sol;base&sol;casts&period;h&gt;`

[source,cpp,subs="verbatim,replacements,macros,-callouts"]
----
template&lt;
    typename To,
    typename From&gt;
&lsqb;&lsqb;nodiscard&rsqb;&rsqb;
To
down&lowbar;cast(From* f);
----

== Description

A &quot;downcast&quot;, i&period;e&period; a conversion from a pointer to an object to a pointer to a more&hyphen;derived object that may contain the original object as a base subobject, cannot safely be done using static&lowbar;cast, because you do not generally know whether the source object is really the base subobject of a containing, more&hyphen;derived object of the target type&period; Thus, when you downcast in a polymorphic type hierarchy, you should use the following function template&period;

This function only returns null when the input is null&period; In debug mode, we use dynamic&lowbar;cast to double&hyphen;check whether the downcast is legal (we die if it&apos;s not)&period; In normal mode, we do the efficient static&lowbar;cast instead&period; Because the process will die in debug mode, it&apos;s important to test to make sure the cast is legal before calling this function!

dynamic&lowbar;cast should be avoided except as allowed by the style guide (https&colon;&sol;&sol;google&period;github&period;io&sol;styleguide&sol;cppguide&period;html&num;Run&hyphen;Time&lowbar;Type&lowbar;Information&lowbar;&lowbar;RTTI&lowbar;)&period;

Use like this&colon; `down&lowbar;cast&lt;T&ast;&gt;(foo)&semi;`

== Return Value

`f`, downcast to `To`, or null if `f` is null&period;

[NOTE]
====
The return value https://en.cppreference.com/cpp/language/attributes/nodiscard[should not be discarded^].
====
== Parameters

[cols="1,4"]
|===
| Name| Description
| *f*
| The source pointer to downcast&period;
|===


[.small]#Created with https://www.mrdocs.com[MrDocs]#
