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

Bart bartjunk64 at gmail.com
Tue Feb 25 20:04:30 CET 2025


Hi,

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.
I would have expected though that foo(nil) would cause a "cannot
determine which overload to call" compiler error. (Which in my
particular case I would have welcomed.)

Q1: do we have some documentation about this overload selection process?
Q2: if not, then is the observed behaviour of current fpc by design or
undefined?

Bart

-- 
Bart


More information about the fpc-devel mailing list