AK: Allow as_if to perform dynamic cast when necessary

This means that if `is<OutputType>(x)` is true, that
`as_if<OutputType>(x)` should always succeed.
This commit is contained in:
Tim Ledbetter 2025-01-31 09:27:05 +00:00 committed by Alexander Kalenik
parent 6ebe19d13b
commit 1f87a09503
Notes: github-actions[bot] 2025-01-31 13:30:59 +00:00

View file

@ -56,7 +56,11 @@ ALWAYS_INLINE CopyConst<InputType, OutputType>* as_if(InputType& input)
{
if (!is<OutputType>(input))
return nullptr;
return static_cast<CopyConst<InputType, OutputType>*>(&input);
if constexpr (IsBaseOf<InputType, OutputType>) {
return static_cast<CopyConst<InputType, OutputType>*>(&input);
} else {
return dynamic_cast<CopyConst<InputType, OutputType>*>(&input);
}
}
template<typename OutputType, typename InputType>