Returning a Struct from a Function
  Home FAQ Contact Sign in
comp.lang.c++ only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.c++ Profile…
 Up
Returning a Struct from a Function         


Author: alex.j.k2
Date: May 11, 2008 10:57

Hello, newbie here.

I'd like to know if there is a way to return a structure from a
function, without creating any other intermediate variables. Also,
this should be done such that the function can still be declared as
inline.

More precisely, given the structure:

struct TwoFields {
int Field_one;
int Field_two;
}

I want a function like the following one, but without the
extra declaration, "TwoFields myReturnStruct;":

inline TwoFields ReturnMyFields(int myInt) {

TwoFields myReturnStruct;

myReturnStruct.Field_one = myInt/2;
myReturnStruct.Field_two = myInt%%2;

return myReturnStruct;
}
Show full article (0.73Kb)
14 Comments
Re: Returning a Struct from a Function         


Author: john
Date: May 11, 2008 11:02

alex.j.k2@gmail.com dixit:
> Hello, newbie here.
>
> I'd like to know if there is a way to return a structure from a
> function, without creating any other intermediate variables. Also,
> this should be done such that the function can still be declared as
> inline.
>

The usual way is to pass a reference to the struct (as a parameter) in
which you want to put the return value (the same for vector, list, ...)
no comments
Re: Returning a Struct from a Function         


Author: alex.j.k2
Date: May 11, 2008 11:18

On May 11, 2:02 pm, john gmail.com> wrote:
> alex.j...@gmail.com dixit:
>
>> Hello, newbie here.
>
>> I'd like to know if there is a way to return a structure from a
>> function, without creating any other intermediate variables. Also,
>> this should be done such that the function can still be declared as
>> inline.
>
> The usual way is to pass a reference to the struct (as a parameter) in
> which you want to put the return value (the same for vector, list, ...)

OK, that certainly works.

But is this the only way to do it?

What if I have a lot of code which returns a struct (or vector,
etc..) by creating an intermediate variable in the function; and I do
not want to modify all the occurrences of the function in the code,
only the function itself.
Show full article (0.94Kb)
no comments
Re: Returning a Struct from a Function         


Author: peter koch
Date: May 11, 2008 11:19

On 11 Maj, 19:57, alex.j...@gmail.com wrote:
> Hello, newbie here.
>
>   I'd like to know if there is a way to return a structure from a
> function, without creating any other intermediate variables. Also,
> this should be done such that the function can still be declared as
> inline.
Show full article (1.29Kb)
no comments
Re: Returning a Struct from a Function         


Author: alex.j.k2
Date: May 11, 2008 11:33

On May 11, 2:19 pm, peter koch gmail.com> wrote:
> On 11 Maj, 19:57, alex.j...@gmail.com wrote:
>
>> Hello, newbie here.
>
>> I'd like to know if there is a way to return a structure from a
>> function, without creating any other intermediate variables. Also,
>> this should be done such that the function can still be declared as
>> inline.
>
> Yes. Any ordinary function will likely do so. The compiler is allowed
> to optimise this special case that has been given the name (N)RVO for
> (Named) Return Value Optimisation and all recent compilers should be
> able to use this strategy.
>
>

If I parse this correctly, you are saying that a good compiler will
not actually make the extra allocation if it is not needed, even if I
use the code in the initial post. Is that correct?
Show full article (1.42Kb)
no comments
Re: Returning a Struct from a Function         


Author: Alf P. Steinbach
Date: May 11, 2008 11:45

* alex.j.k2@gmail.com:
> On May 11, 2:02 pm, john gmail.com> wrote:
>> alex.j...@gmail.com dixit:
>>
>>> Hello, newbie here.
>>> I'd like to know if there is a way to return a structure from a
>>> function, without creating any other intermediate variables. Also,
>>> this should be done such that the function can still be declared as
>>> inline.
>> The usual way is to pass a reference to the struct (as a parameter) in
>> which you want to put the return value (the same for vector, list, ...)
>
> OK, that certainly works.
>
> But is this the only way to do it?
>
> What if I have a lot of code which returns a struct (or vector,
> etc..) by creating an intermediate variable in the function; and I do
> not want to modify all the occurrences of the function in the code,
> only the function itself. ...
Show full article (3.77Kb)
no comments
Re: Returning a Struct from a Function         


Author: alex.j.k2
Date: May 11, 2008 12:36

On May 11, 2:45 pm, "Alf P. Steinbach" wrote:
> * alex.j...@gmail.com:
>
>> Can I eliminate the creation of the extra variable, while returning
>> the struct (but not as a parameter) ?
>
> In general one should not optimize prematurely. However, designing a
> program so that it needlessly copies megabytes of data in function
> calls is ungood. The advice about optimization is really about
> micro-optimization, not about not applying common sense.
>

Yes. In my defense, I did not do something as dumb as passing
megabytes of data by value instead of reference. The values passed
around are just one integer and a two integer struct, as in my
example. Google led me to believe that there is not much speed
difference in passing by reference instead of value for such small
data -- sometimes (for some compilers) passing by value may be faster,
Google said.
Show full article (4.08Kb)
no comments
Re: Returning a Struct from a Function         


Author: Gianni Mariani
Date: May 11, 2008 15:33

alex.j.k2@gmail.com wrote:
> Hello, newbie here.
>
> I'd like to know if there is a way to return a structure from a
> function, without creating any other intermediate variables. Also,
> this should be done such that the function can still be declared as
> inline.
>
> More precisely, given the structure:
>
> struct TwoFields {
> int Field_one;
> int Field_two;
> }
>
> I want a function like the following one, but without the
> extra declaration, "TwoFields myReturnStruct;":
>
> inline TwoFields ReturnMyFields(int myInt) {
> ...
Show full article (1.33Kb)
no comments
Re: Returning a Struct from a Function         


Author: Erik Wikström
Date: May 12, 2008 09:31

On 2008-05-12 00:33, Gianni Mariani wrote:
> alex.j.k2@gmail.com wrote:
>> Hello, newbie here.
>>
>> I'd like to know if there is a way to return a structure from a
>> function, without creating any other intermediate variables. Also,
>> this should be done such that the function can still be declared as
>> inline.
>>
>> More precisely, given the structure:
>>
>> struct TwoFields {
>> int Field_one;
>> int Field_two;
>> }
>>
>> I want a function like the following one, but without the
>> extra declaration, "TwoFields myReturnStruct;":
>>
>> inline TwoFields ReturnMyFields(int myInt) { ...
Show full article (1.13Kb)
no comments
Re: Returning a Struct from a Function         


Author: Victor Bazarov
Date: May 12, 2008 09:55

Stefan Ram wrote:
> =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?= telia.com> writes:
>>>> inline TwoFields ReturnMyFields(int myInt) {
>>>> TwoFields myReturnStruct; [...]
>>>> return myReturnStruct;
>> const TwoFields & f = ReturnMyFields(v);
>
> How long will the object created with
no comments

RELATED THREADS
SubjectArticles qty Group
Hypergeometric functions and beta functionssci.math ·
#43449 [NEW]: Segmentation Fault when calling PL/SQL-function wich returns ref cursormailing.www.phpdev ·
Access 97 using ODBC returning SQL server ntext fields dont return the full datamicrosoft.public.sqlserver.odbc ·
 
1 2