Is there a way to avoid duplication and improve readability on template functions return type ?
Here is an example
template <typename FunctionType>
std::enable_if_t<
!std::is_void_v<std::invoke_result_t<FunctionType, MyClass*>>,
std::optional<std::invoke_result_t<FunctionType, MyClass*>>
> CallIfValid(MyClass* instance, FunctionType func)
{
using InvocationType = std::invoke_result_t<FunctionType, MyClass*>;
if (instance != nullptr)
{
return func(instance);
}
else
{
return std::optional<InvocationType>();
}
}
Notice how std::invoke_result_t<FunctionType, MyClass*>
ends up duplicated twice in return type, and also a third time in method body.
Any suggestions or trick I am not seeing here ?
Thanks
Aucun commentaire:
Enregistrer un commentaire