Unittests and extern(C)

H. S. Teoh via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 22 11:18:00 PDT 2017


On Thu, Jun 22, 2017 at 10:16:51AM +0200, Jacob Carlborg via Digitalmars-d wrote:
> On 2017-06-22 00:19, H. S. Teoh via Digitalmars-d wrote:
> 
> > The code template says:
> > 
> > 	extern(C) double funcImpl(double x, double y)
> > 
> > But the function pointer type is declared as:
> > 
> > 	alias FuncImpl = double function(double, double);
> > 
> > Notice the lack of `extern(C)` in the latter.
[...]
> You could also add a template that generates a function pointer based
> on the actual function, "funcImpl" in this case. Something like:
> 
> extern(C) double funcImpl(double x, double y);
> 
> alias FuncImpl = generateFunctionPointer!(funcImpl);
> 
> generateFunctionPointer would evaluate to a function pointer type with
> the correct signature and calling conventions by inspecting the passed
> in function symbol. This would avoid that the signatures get out of
> sync and is a bit more DRY as well.
[...]

Unfortunately, funcImpl's declaration doesn't exist in the main program;
it's part of a string used for generating the source code for the shared
library.  I *could* factor it out from the string, I suppose, and use a
mixin to extract the type of its function pointer.

Hmm, come to think of it, maybe that's what I'll do!  The fact that you
can do this at all (i.e., use the same string to generate both the
output file and an in-language declaration) is a testament to just how
cool D is.  In C/C++ you could use macros to achieve the same thing via
stringization, I suppose, but it's uglier.


T

-- 
2+2=4. 2*2=4. 2^2=4. Therefore, +, *, and ^ are the same operation.


More information about the Digitalmars-d mailing list