public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Passing a reference to a value returned by a function
@ 2003-03-11 16:03 Mihnea Balta
  2003-03-11 16:23 ` Oliver Kullmann
  0 siblings, 1 reply; 5+ messages in thread
From: Mihnea Balta @ 2003-03-11 16:03 UTC (permalink / raw)
  To: gcc-help

Hi.

I have this piece of code (excuse the long function names, it's example code 
:) ):

#include <stdio.h>

struct type_t{
    int a, b, c;
};

type_t function_returning_struct(){
    type_t  a;

    a.a = 1;
    return a;
}

int function_taking_reference(type_t& a){
    if(a.a == 1)
        return 1;
    else
        return 0;
}

int main(){
    printf("%d\n", function_taking_reference(function_returning_struct()));
    return 1;
}

I'm trying to port code written like this from visualc to gcc. I suppose that 
visualc compiles the above piece of code because it creates a temporary 
type_t that holds the return of function_returning_struct(), and passes a 
reference to that to function_taking_reference(). However, gcc 3.2.2 won't 
compile that kind of code.

Is there any way to make gcc work with such code? I'm asking because it's in a 
lot of places, and adding a temp var by hand to all the places where this 
stuff occurs would be a pain.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Passing a reference to a value returned by a function
  2003-03-11 16:03 Passing a reference to a value returned by a function Mihnea Balta
@ 2003-03-11 16:23 ` Oliver Kullmann
  2003-03-11 17:41   ` Mihnea Balta
  0 siblings, 1 reply; 5+ messages in thread
From: Oliver Kullmann @ 2003-03-11 16:23 UTC (permalink / raw)
  To: Mihnea Balta; +Cc: gcc-help

On Tue, Mar 11, 2003 at 05:56:34PM +0200, Mihnea Balta wrote:
> Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm
> Precedence: bulk
> List-Unsubscribe: <mailto:gcc-help-unsubscribe-O.Kullmann=swansea.ac.uk@gcc.gnu.org>
> List-Archive: <http://gcc.gnu.org/ml/gcc-help/>
> List-Post: <mailto:gcc-help@gcc.gnu.org>
> List-Help: <mailto:gcc-help-help@gcc.gnu.org>
> Delivered-To: mailing list gcc-help@gcc.gnu.org
> From: Mihnea Balta <dark_lkml@mymail.ro>
> To: gcc-help@gcc.gnu.org
> Date: Tue, 11 Mar 2003 17:56:34 +0200
> Subject: Passing a reference to a value returned by a function
> X-Spam-Status: No, hits=0.0 required=8.0
> 	tests=none
> 	version=2.50
> X-Spam-Level: 
> X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp)
> X-MIME-Autoconverted: from quoted-printable to 8bit by cs-svr1.swan.ac.uk id h2BFw0uE014171
> 
> Hi.
> 
> I have this piece of code (excuse the long function names, it's example code 
> :) ):
> 
> #include <stdio.h>
> 
> struct type_t{
>     int a, b, c;
> };
> 
> type_t function_returning_struct(){
>     type_t  a;
> 
>     a.a = 1;
>     return a;
> }
> 
> int function_taking_reference(type_t& a){
>     if(a.a == 1)
>         return 1;
>     else
>         return 0;
> }
> 
> int main(){
>     printf("%d\n", function_taking_reference(function_returning_struct()));
>     return 1;
> }
> 
> I'm trying to port code written like this from visualc to gcc. I suppose that 
> visualc compiles the above piece of code because it creates a temporary 
> type_t that holds the return of function_returning_struct(), and passes a 
> reference to that to function_taking_reference(). However, gcc 3.2.2 won't 
> compile that kind of code.
> 
> Is there any way to make gcc work with such code? I'm asking because it's in a 
> lot of places, and adding a temp var by hand to all the places where this 
> stuff occurs would be a pain.
> 
> Thanks.

Hi,

I hope very much that there is no way to make gcc to work with such code,
since it doesn't make sense to pass temporary values by reference, and the
standards explicitely forbids it. But you don't have to add variables ---
just add the const qualifier to the argument type of function_taking_reference,
and you have perfectly legal C++ code. Doing so doesn't change your code,
but just increases safety.

Oliver

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Passing a reference to a value returned by a function
  2003-03-11 16:23 ` Oliver Kullmann
@ 2003-03-11 17:41   ` Mihnea Balta
  0 siblings, 0 replies; 5+ messages in thread
From: Mihnea Balta @ 2003-03-11 17:41 UTC (permalink / raw)
  To: Oliver Kullmann; +Cc: gcc-help

Thanks, it works with the const qualifier.
My next question would have been about how much visualc's behavior respects 
the standard, thanks for answering that in advance ;)

On Tuesday 11 March 2003 18:16, Oliver Kullmann wrote:
> On Tue, Mar 11, 2003 at 05:56:34PM +0200, Mihnea Balta wrote:
> > Mailing-List: contact gcc-help-help@gcc.gnu.org; run by ezmlm
> > Precedence: bulk
> > List-Unsubscribe:
> > <mailto:gcc-help-unsubscribe-O.Kullmann=swansea.ac.uk@gcc.gnu.org>
> > List-Archive: <http://gcc.gnu.org/ml/gcc-help/>
> > List-Post: <mailto:gcc-help@gcc.gnu.org>
> > List-Help: <mailto:gcc-help-help@gcc.gnu.org>
> > Delivered-To: mailing list gcc-help@gcc.gnu.org
> > From: Mihnea Balta <dark_lkml@mymail.ro>
> > To: gcc-help@gcc.gnu.org
> > Date: Tue, 11 Mar 2003 17:56:34 +0200
> > Subject: Passing a reference to a value returned by a function
> > X-Spam-Status: No, hits=0.0 required=8.0
> > 	tests=none
> > 	version=2.50
> > X-Spam-Level:
> > X-Spam-Checker-Version: SpamAssassin 2.50 (1.173-2003-02-20-exp)
> > X-MIME-Autoconverted: from quoted-printable to 8bit by cs-svr1.swan.ac.uk
> > id h2BFw0uE014171
> >
> > Hi.
> >
> > I have this piece of code (excuse the long function names, it's example
> > code
> >
> > :) ):
> >
> > #include <stdio.h>
> >
> > struct type_t{
> >     int a, b, c;
> > };
> >
> > type_t function_returning_struct(){
> >     type_t  a;
> >
> >     a.a = 1;
> >     return a;
> > }
> >
> > int function_taking_reference(type_t& a){
> >     if(a.a == 1)
> >         return 1;
> >     else
> >         return 0;
> > }
> >
> > int main(){
> >     printf("%d\n",
> > function_taking_reference(function_returning_struct())); return 1;
> > }
> >
> > I'm trying to port code written like this from visualc to gcc. I suppose
> > that visualc compiles the above piece of code because it creates a
> > temporary type_t that holds the return of function_returning_struct(),
> > and passes a reference to that to function_taking_reference(). However,
> > gcc 3.2.2 won't compile that kind of code.
> >
> > Is there any way to make gcc work with such code? I'm asking because it's
> > in a lot of places, and adding a temp var by hand to all the places where
> > this stuff occurs would be a pain.
> >
> > Thanks.
>
> Hi,
>
> I hope very much that there is no way to make gcc to work with such code,
> since it doesn't make sense to pass temporary values by reference, and the
> standards explicitely forbids it. But you don't have to add variables ---
> just add the const qualifier to the argument type of
> function_taking_reference, and you have perfectly legal C++ code. Doing so
> doesn't change your code, but just increases safety.
>
> Oliver
>
>
> ------------------------------------------------------------------------
> http://felicitari.mymail.ro/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Passing a reference to a value returned by a function
  2003-03-11 16:07 Ajay Bansal
@ 2003-03-11 16:16 ` Mihnea Balta
  0 siblings, 0 replies; 5+ messages in thread
From: Mihnea Balta @ 2003-03-11 16:16 UTC (permalink / raw)
  To: Ajay Bansal, gcc-help

Yes, that's what I meant with "adding a temp var by hand to all the places 
where this stuff occurs" :). I'd like to have as little modification as 
possible to the original code, since that kind of construct occurs very 
often.

I can't make the functions take structs instead of references either, because 
it's a critical section of the code, and the strctures are pretty big.

On Tuesday 11 March 2003 18:05, Ajay Bansal wrote:
> One way id to create a variable & store the return value of
> function_returning_struct in this variable as
>
>  type_t a = function_returning_struct();
>     printf("%d\n", function_taking_reference(a));
>     return 1;
>
> -----Original Message-----
> From: Mihnea Balta [mailto:dark_lkml@mymail.ro]
> Sent: Tuesday, March 11, 2003 9:27 PM
> To: gcc-help@gcc.gnu.org
> Subject: Passing a reference to a value returned by a function
>
>
> Hi.
>
> I have this piece of code (excuse the long function names, it's example
> code
>
> :) ):
>
> #include <stdio.h>
>
> struct type_t{
>     int a, b, c;
> };
>
> type_t function_returning_struct(){
>     type_t  a;
>
>     a.a = 1;
>     return a;
> }
>
> int function_taking_reference(type_t& a){
>     if(a.a == 1)
>         return 1;
>     else
>         return 0;
> }
>
> int main(){
>     printf("%d\n",
> function_taking_reference(function_returning_struct()));
>     return 1;
> }
>
> I'm trying to port code written like this from visualc to gcc. I suppose
> that
> visualc compiles the above piece of code because it creates a temporary
> type_t that holds the return of function_returning_struct(), and passes
> a
> reference to that to function_taking_reference(). However, gcc 3.2.2
> won't
> compile that kind of code.
>
> Is there any way to make gcc work with such code? I'm asking because
> it's in a
> lot of places, and adding a temp var by hand to all the places where
> this
> stuff occurs would be a pain.
>
> Thanks.
>
>
> ------------------------------------------------------------------------
> http://felicitari.mymail.ro/

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: Passing a reference to a value returned by a function
@ 2003-03-11 16:07 Ajay Bansal
  2003-03-11 16:16 ` Mihnea Balta
  0 siblings, 1 reply; 5+ messages in thread
From: Ajay Bansal @ 2003-03-11 16:07 UTC (permalink / raw)
  To: Mihnea Balta, gcc-help

One way id to create a variable & store the return value of
function_returning_struct in this variable as

 type_t a = function_returning_struct();
    printf("%d\n", function_taking_reference(a));
    return 1;

-----Original Message-----
From: Mihnea Balta [mailto:dark_lkml@mymail.ro] 
Sent: Tuesday, March 11, 2003 9:27 PM
To: gcc-help@gcc.gnu.org
Subject: Passing a reference to a value returned by a function


Hi.

I have this piece of code (excuse the long function names, it's example
code 
:) ):

#include <stdio.h>

struct type_t{
    int a, b, c;
};

type_t function_returning_struct(){
    type_t  a;

    a.a = 1;
    return a;
}

int function_taking_reference(type_t& a){
    if(a.a == 1)
        return 1;
    else
        return 0;
}

int main(){
    printf("%d\n",
function_taking_reference(function_returning_struct()));
    return 1;
}

I'm trying to port code written like this from visualc to gcc. I suppose
that 
visualc compiles the above piece of code because it creates a temporary 
type_t that holds the return of function_returning_struct(), and passes
a 
reference to that to function_taking_reference(). However, gcc 3.2.2
won't 
compile that kind of code.

Is there any way to make gcc work with such code? I'm asking because
it's in a 
lot of places, and adding a temp var by hand to all the places where
this 
stuff occurs would be a pain.

Thanks.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-03-11 16:23 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-03-11 16:03 Passing a reference to a value returned by a function Mihnea Balta
2003-03-11 16:23 ` Oliver Kullmann
2003-03-11 17:41   ` Mihnea Balta
2003-03-11 16:07 Ajay Bansal
2003-03-11 16:16 ` Mihnea Balta

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).