How to create a function declaration?
Frits van Bommel
fvbommel at REMwOVExCAPSs.nl
Fri Jan 12 12:51:10 PST 2007
Marcio Faustino wrote:
> Frits van Bommel wrote:
>> 'printf' is problematic because object.d (which is automatically
>> imported into every D module) also declares it.
>>
>> That code might otherwise compile, but I'm not sure what would happen at
>> the linking stage; printf is also defined in the C runtime library which
>> is linked to by default. It might just use one of the two, or it might fail.
>
> But the problem is that the code *does not* compile with the line that says
> "Error" uncommented. Without getting into linking details, the compiler *should*
> accept the given definition because "object.d" and "internal/object.d" only
> contain declarations of the printf function.
>
> Am I wrong?
The compiler sees two declarations for printf, in different modules. It
sees these as different functions as one is 'object.printf' and the
other is 'my_printf.printf'. The easiest ways to solve this ambiguity are
(1) rename your printf ;).
(2) add 'alias my_printf.printf printf' to your test.d to make it clear
which printf you want.
(3) change the import line to 'import my_printf : printf' to import only
printf from that module and additionally get the effect of (2).
I know it seems a bit silly to have to disambiguate between two
declarations that will generate the exact same machine code when called
(including the called address) but D's name resolution was probably
designed with name mangling in mind...
More information about the Digitalmars-d-learn
mailing list