[fpc-devel] Question on function overloading and pointer types

Tomas Hajny XHajT03 at hajny.biz
Tue Feb 25 22:35:07 CET 2025


On 2025-02-25 20:04, Bart via fpc-devel wrote:


Hi Bart,

> I possibly did not search hard enough, but did not find what the
> "rules" are regarding overload selection (is that the correct term?).
> 
> Consider this simple example:
> ===
> program test;
> {$ifdef fpc}
> {$mode fpc}
> {$endif fpc}
> 
> {$apptype console}
> {$ifndef fpc}
> type
>   TProcedure = procedure;
> {$endif not pc}
> 
> procedure dummyproc;
> begin
> end;
> 
> procedure Foo(buf: pointer); {$ifndef fpc}overload;{$endif not fpc}
> begin
>   writeln('Foo(buf: pointer)');
> end;
> 
> procedure foo(proc: TProcedure); {$ifndef fpc}overload;{$endif not fpc}
> begin
>   writeln('Foo(proc: TProcedure)');
> end;
> 
> var
>   x: integer;
> begin
>   write('foo(@x) -> ');
>   foo(@x);
>   write('foo(@dummyproc) -> ');
>   foo(@dummyproc);
>   write('foo(nil) -> ');
>   foo(nil);
> end.
> ====
> 
> fpc outputs:
> foo(@x) -> Foo(buf: pointer)
> foo(@dummyproc) -> Foo(proc: TProcedure)
> foo(nil) -> Foo(buf: pointer)
> 
> Delphi 7 (yes, very old, probably not a good reference):
> foo(@x) -> Foo(buf: pointer)
> foo(@dummyproc) -> Foo(buf: pointer)
> foo(nil) -> Foo(buf: pointer)
> 
> So, fpc is more clever than D7.
  .
  .

No, it's because you used a different compilation mode _and_ FPC treats 
@ differently depending on the compilation mode. If you change "{$mode 
fpc}" to "{$mode delphi}" and make the "overload" directive 
non-conditional (otherwise FPC rejects to compile the source in that 
mode), you get the same result with FPC as with your Delphi 7. If you 
change "foo(@dummyproc)" to "foo(dummyproc)" and recompile it (still 
with {$mode delphi}), you get the same result as with the original 
version in {$mode fpc} - I guess that Delphi 7 might give you the same 
result, but I might be wrong.

Tomas


More information about the fpc-devel mailing list