| Re: Returning a Struct from a Function |
|
 |
|
 |
|
 |
|
 |
Group: comp.lang.c++ · Group Profile
Author: Erik WikströmErik Wikström Date: May 12, 2008 09:31
On 2008-05-12 00:33, Gianni Mariani 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) {
>>
>> TwoFields myReturnStruct;
>>
>> myReturnStruct.Field_one = myInt/2;
>> myReturnStruct.Field_two = myInt%%2;
>>
>> return myReturnStruct;
>> }
>>
>> The only reason I want this is speed efficiency, so if this is not
>> worth doing please let me know.
>
> Premture optimization alert !
>
> BTW - if you really want to do this - imho this is the easy way - and
> BTW - RVO will prolly happen.
>
> TwoFields & f = ReturnMyFields(v);
const TwoFields & f = ReturnMyFields(v);
--
Erik Wikström
|