https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58051
--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> --- I wonder if it's this simple: --- a/gcc/cp/typeck.c +++ b/gcc/cp/typeck.c @@ -8618,8 +8618,6 @@ check_return_expr (tree retval, bool *no_warning) || TREE_CODE (retval) == PARM_DECL) && DECL_CONTEXT (retval) == current_function_decl && !TREE_STATIC (retval) - && same_type_p ((TYPE_MAIN_VARIANT (TREE_TYPE (retval))), - (TYPE_MAIN_VARIANT (functype))) /* This is only interesting for class type. */ && CLASS_TYPE_P (functype)) flags = flags | LOOKUP_PREFER_RVALUE;