Passing a reference to a value returned by a function

Mihnea Balta dark_lkml@mymail.ro
Tue Mar 11 16:03:00 GMT 2003


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.



More information about the Gcc-help mailing list