Arthur Schwarz | 6 Jul 2012 19:21
Picon
Favicon

Method stack return usage has incorrect GCC error


IDE Netbeans 7.2RC1   GCC 4.5.3 compiled under Cygwin

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.

The code contains several examples of usage for function arguments. When a 
stacked object, derived b1(++y);"" and  ' derived  b3 = b1.fnc1();'",  is used 
as an argument to a function,  b1.fnc4(b3);"" and " b1.fnc4(b1);",  no error is 
generated. The diagnostic only occurs when the return argument fro a function is 
used as an argument to another function.

Not that this is of the slightest concern to GCC (nor should it be), MSVC++ 2010 
does not produce a diagnostis.

thanks
art

# include <cstdlib>
# include <iostream>
# include <iomanip>

using namespace std;

static int y = 0;

class base {
(Continue reading)

Jonathan Wakely | 6 Jul 2012 19:28
Picon

Re: Method stack return usage has incorrect GCC error

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.

Jonathan Wakely | 6 Jul 2012 19:29
Picon

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

Arthur Schwarz | 6 Jul 2012 20:13
Picon
Favicon

Re: Method stack return usage has incorrect GCC error

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 <at> gmail.com>
To: Arthur Schwarz <aschwarz1309 <at> att.net>
Cc: gcc-help <at> 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
(Continue reading)


Gmane