Passing static array to C variadic function
Daniel Gibson via Digitalmars-d
digitalmars-d at puremagic.com
Sun Jul 20 12:06:19 PDT 2014
Am 20.07.2014 18:37, schrieb bearophile:
> Daniel Gibson:
>
>> For "normal" functions http://dlang.org/interfaceToC.html tells me to
>> add a "ref" in the function signature, to tell D to pass it by
>> reference (couldn't this be implicit for extern (C) functions?)
>
> I don't know why D isn't adapting such things to the needs of C.
>
> Bye,
> bearophile
Yeah, IMHO it would indeed make sense to make extern (C) functions
behave more like C.
They already are different to D functions (different name mangling and,
more visibly to the user, varargs behave differently), so why not do it
properly?
As far as I see it, passing static arrays by value to C functions is
never what you want because C just doesn't support it, so why not make
it implicit? This also goes the other way round: if I implement an
extern (C) function in D (vs just having a function pointer or a
declaration of a function implemented in C), D "thinks" it's getting a
static array by value when it's really just getting a ref..
And for the varargs case (where I can't just add "ref"), it's totally
non-obvious that I have to pass staticArr.ptr instead, so here it would
be even more desirable to have that done implicitly.
So what I (naive as I am) would expect extern (C) to do to functions is:
* C name mangling (that's done)
* C-style varargs (also done)
* passing stuff to the function is done as C expects it (not done,
also: are there other cases than the static array one that are
different?)
* Disallowing template arguments (because how would I declare and call
that function in C?) (not done)
* /Maybe/ disallowing types as arguments/return types that are not
supported by C (D classes; not done)?
(OTOH, one the C side one could just handle them as void* and pass
instances around opaquely)
Cheers,
Daniel
More information about the Digitalmars-d
mailing list