Recent

Author Topic: Conversion question about C++ about variable chaining assignments.  (Read 788 times)

jamie

  • Hero Member
  • *****
  • Posts: 7603
Conversion question about C++ about variable chaining assignments.
« on: September 01, 2024, 02:37:34 am »
I came into a questionable situation where I thought I was sure but now scratching my head..

consider this
Code: Pascal  [Select][+][-]
  1.  
  2. Variable1 = Variable2 = variable2 = Function:ReturnAsingleVariable;
  3.  

 I know normally you can assign multiple variables from a single item like that, however, this is from a function!

 how many times does that function get called? Once or three times?

Thank you.


The only true wisdom is knowing you know nothing

440bx

  • Hero Member
  • *****
  • Posts: 6155
Re: Conversion question about C++ about variable chaining assignments.
« Reply #1 on: September 01, 2024, 05:01:26 am »
I know normally you can assign multiple variables from a single item like that, however, this is from a function!
I guess it depends on the compiler but, strictly speaking the compiler should only call the function _once_ to determine the value that must be propagated.

IOW, the compiler shouldn't care that the value is assigned to one or multiple variables.   The algorithm should be the same, i.e, 1. determine the value to assign 2. propagate the value to the variables.  done.

If the compiler does something different than that, it would be a bug.

It's crucial for the compiler to call the function only once because if the function has side effects then the variables would be assigned different values.  Therefore, it is a bug for the compiler to call the function multiple times since the semantics of the statement require all variables to have the same value and the compiler _must_ guarantee that.

HTH.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

jamie

  • Hero Member
  • *****
  • Posts: 7603
Re: Conversion question about C++ about variable chaining assignments.
« Reply #2 on: September 01, 2024, 05:08:59 am »
Ok, Thanks.

  I've seen this several times in the sources but this time, it happens to be coming from a streaming function.

   Apparently, the intention is to use a single value read from that streaming function and initiate all the variables with that.

Have a good day.
 :D
The only true wisdom is knowing you know nothing

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1576
    • Lebeau Software
Re: Conversion question about C++ about variable chaining assignments.
« Reply #3 on: September 02, 2024, 03:58:02 am »
how many times does that function get called? Once or three times?

The function is called only one time. The return value is assigned to variable2, then its value is assigned to Variable2, then its value is assigned to Variable1.
 
I guess it depends on the compiler

No, not really. The C++ standard dictates that the result of the built-in assignment operator for native types is the value being assigned. For objects, it is a reference to the object being modified (unless the user overrides that operator to return something else).
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

 

TinyPortal © 2005-2018