Forum > Beginners

[Solved] Is it possible to const an argument passed by reference in FPC?

<< < (3/13) > >>

marcov:
Note that the refness of const depends on type and ABI.

That said, I only think the differences will be in reggable quantities, like GUID, for which constref was made (iunknown declaration).  It's 128-bit, but that is maybe reggable with 2 64-bits regs on some archs

lucamar:

--- Quote from: marcov on July 24, 2019, 02:17:32 pm ---Note that the refness of const depends on type and ABI.
--- End quote ---

Indeed, wasn't that one the reasons to introduce constref? To make absolutely sure that the parameter is passed as a strict reference?

440bx:

--- Quote from: ASerge on July 24, 2019, 02:10:40 pm ---
--- Quote from: 440bx on July 24, 2019, 10:51:45 am ---For strings, that is only true when LONGSTRINGS ON is in effect and the parameter is not a shortstring.

--- End quote ---

--- End quote ---
That statement is 100% correct.

The reason I rejected it (or better stated, wanted to clarify it)

--- Quote from: marcov on July 24, 2019, 10:38:47 am ---strings and dynamic arrays already contain an implicit ref.

--- End quote ---
There is NO implicit reference if a function parameter is a shortsting, either because of LONGSTRINGS OFF or explicitly specifying the type as shortstring and, neither const nor constref is specified, it will NOT be passed by reference, IOW, there is no implicit ref


--- Quote from: ASerge on July 24, 2019, 02:10:40 pm ---And after I made it clear that it was wrong, you agreed with @marcov.

--- End quote ---
What I posted was not wrong at all.  It was and, still is correct.  The code I posted along with the disassembly proves it.  There is no implicit ref, the parameter will be copied, i.e, passed by value.

That said, you posted an example were you used "const" which for strings happens to be the same as "constref".  What you showed in the example/code you presented is correct BUT your example EXPLICITLY, by the use of "const," is indicating that the parameter is passed by reference.  It is NOT implicit as Marco stated.


--- Quote from: ASerge on July 24, 2019, 02:10:40 pm ---
--- Quote from: 440bx on July 24, 2019, 12:51:25 pm ---No.  The reason you are getting that is because you specified "const" in the parameter, which for strings happens to be the same as specifying constref,

--- End quote ---
Good answer :(

--- End quote ---
Things got mixed up and I am in part responsible.  Marco, in his post, also referred to "const" which has the effect of passing the string by reference (as you showed) so I have to agree with that part. 

The part, which is the initial point I made, is that there is no implicit ref when passing strings when LONGSTRINGS is OFF or when passing a shortstring.

Hopefully, this is straight now.  There is no implicit ref when passing strings to a function either when LONGSTRINGS is OFF or the parameter is a shortstring.


ETA:


--- Quote from: marcov on July 24, 2019, 02:17:32 pm ---Note that the refness of const depends on type and ABI.

--- End quote ---
That's part of the problem too.  The Programmer's reference guide warns about "const" not meaning "pass by reference" (as it is in Delphi) but, it doesn't say anything about when it implies "pass by reference" and when it doesn't. Basically, if you want to know, look at the assembly code and figure it out yourself.

PascalDragon:

--- Quote from: 440bx on July 24, 2019, 02:40:06 pm ---
--- Quote from: marcov on July 24, 2019, 02:17:32 pm ---Note that the refness of const depends on type and ABI.

--- End quote ---
That's part of the problem too.  The Programmer's reference guide warns about "const" not meaning "pass by reference" (as it is in Delphi) but, it doesn't say anything about when it implies "pass by reference" and when it doesn't. Basically, if you want to know, look at the assembly code and figure it out yourself.

--- End quote ---
Also the behaviour of const might change with each version of FPC as it's not part of the platform ABI, thus it might be that in some newer version it's decided to pass a parameter type in a more optimal way.

440bx:

--- Quote from: PascalDragon on July 25, 2019, 09:55:53 am ---Also the behaviour of const might change with each version of FPC as it's not part of the platform ABI, thus it might be that in some newer version it's decided to pass a parameter type in a more optimal way.

--- End quote ---
I don't know if you will agree or not but, personally, I think that "const" should never be used with parameters.  "var" and "constref", yes, because they are well defined. "const" no, because its meaning is, in addition to being anyone's guess, as you pointed out it might change in the future.

If it weren't needed for Delphi compatibility, I'd even suggest not allowing "const" with parameters.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version