AK: Avoid double "is" check in as_if<T> when type must be dynamic_casted

Before this change, the is<T> check simplified down to a dynamic_cast,
which meant we did two dynamic_casts in a row for no reason.
This commit is contained in:
Andreas Kling 2025-12-15 19:47:29 -06:00 committed by Andreas Kling
parent 95a9ea68a0
commit b7ba863bf7

View file

@ -38,9 +38,9 @@ ALWAYS_INLINE bool is(NonnullRefPtr<InputType> const& input)
template<typename OutputType, typename InputType>
ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType& input)
{
if (!is<OutputType>(input))
return nullptr;
if constexpr (requires { static_cast<CopyConst<InputType, OutputType>*>(&input); }) {
if (!is<OutputType>(input))
return nullptr;
return static_cast<CopyConst<InputType, OutputType>*>(&input);
}
return dynamic_cast<CopyConst<InputType, OutputType>*>(&input);