octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: move constructors likely a requirement


From: Daniel J Sebald
Subject: Re: move constructors likely a requirement
Date: Wed, 21 Aug 2019 01:31:16 -0400
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0

On 8/21/19 1:03 AM, Carlo De Falco wrote:
Hi,

Il giorno 21 ago 2019, alle ore 00:59, Rik <address@hidden> ha scritto:

main ()
{
   comp_class cobj;

   cobj = init ();

   ...
}

comp_class init (void)
{
   comp_class retval;  // local variable

   retval.xxx = yyy;   // initialization
   ...

   return retval;      // return by value (a comp_class object)
}

This may be a naive comment as it is based only on your super-simplified
and I didn't look at the actual code, but wouldn't it be more simple and 
efficient
to change the code pattern above to the following?


main ()
{
   comp_class cobj;

   init (cobj);

   ...
}

void init (comp_class& retval)
{
   //comp_class retval;  // local variable, no longer needed

   retval.xxx = yyy;   // initialization
   ...

   //return retval;      // return by value (a comp_class object), no longer 
needed
}


this is mainly out of curiosity, sorry for the noise if it makes no sense ...

c.


It makes sense; it's one of the alternatives Rik mentioned: "return by reference". That would be my preference, I guess. I spent decades thinking in terms of pointers. Then references came along and I was alright with them--just a little extra sheen. But as features have been added to C++, I always feel a bit more detached and unsure exactly how efficient something compiles.

In C and early C++ the return options were limited to basic variables, and often there were rules about just what particular registers these variable types were stored in... enabling one to be even more efficient at things if the values could be kept within the register without having to go to memory/stack and back.

Dan



reply via email to

[Prev in Thread] Current Thread [Next in Thread]