[fpc-devel] Question on constref

Michael Van Canneyt michael at freepascal.org
Thu Feb 2 10:15:27 CET 2023



On Thu, 2 Feb 2023, Ondrej Pokorny via fpc-devel wrote:

> On 02.02.2023 07:42, Sven Barth via fpc-devel wrote:
>> The case when you *need* a constant reference. Case in point: the 
>> passing of TGuid in IInterface.QueryInterface. Delphi code relies on 
>> it being a reference, but “const” does not guarantee that for all 
>> platforms.
>
> Maybe I am missing something, could you please explain why 
> IInterface.QueryInterface needs constref?

IIRC Joost introduced it at some point, but I don't remember exactly why.

I seem to remember constref was introduced as the equivalent of const * parameters in C.
It allows to skip the pointer.

>       function TObject.GetInterface(const iid : tguid;out obj) : boolean;
>
> that has a const without ref.
>
> ---
>
> I myself cannot think of any real use case of constref other than hacks 
> like the FreeAndNil in recent Delphi versions:
>
> procedure FreeAndNil(const [ref] Obj: TObject);
>
> that needs the ref because it changes it to nil (so is not really a 
> const - it used to be an untyped var). As a result FreeAndNil allows you 
> to happily pass read-only properties to it that are then nilled.

Which is a clear violation of the concept of Const... :/

Michael.


More information about the fpc-devel mailing list