Method stack return usage has incorrect GCC error

Arthur Schwarz aschwarz1309@att.net
Fri Jul 6 18:13:00 GMT 2012


Thanks.

The referenced citation says that you can't pass a reference to an anonymous 
object, and a function return is certainly anonymous, and "(t)he error is that a 
temporary cannot be bound to a non-const reference". the solution in this case 
is to change the prototype to "void fnc4(const derived& d)" to make the boo-boo 
go away.

I think I understand, unlike the Shadow who only "knows", and I appreciate your 
help.

thanks
art



----- Original Message ----
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Arthur Schwarz <aschwarz1309@att.net>
Cc: gcc-help@gcc.gnu.org
Sent: Fri, July 6, 2012 10:29:37 AM
Subject: Re: Method stack return usage has incorrect GCC error

On 6 July 2012 18:28, Jonathan Wakely wrote:
> On 6 July 2012 18:21, Arthur Schwarz wrote:
>>
>> In the following example I think that gcc has incorrectly generated an 
"error:
>> no matching function for call to ‘derived::fnc4(derived)’" error for "
>> b1.fnc4(b1.fnc1());". My guess is that gcc does not handle a function return
>> returning an object on the stack used as an argument to a function. If this 
is
>> incorrect, on my part, would someone explain why I am wrong.
>
> Your function returns an rvalue which cannot bind to a non-const
> lvalue-reference.
>
> i.e. an lvalue reference such as "derived&" cannot bind to a
> temporary, such as the return value of your function.
>
>> Not that this is of the slightest concern to GCC (nor should it be), MSVC++ 
>>2010
>> does not produce a diagnostis.
>
> This is a well-known bug^W feature of  MSVC, I think there's some
> switch to make it follow the standard and disable it.

See e.g. 
http://stackoverflow.com/questions/3897702/why-does-msvc-let-me-do-this-but-not-gcc-g



More information about the Gcc-help mailing list