Unittests and extern(C)

Jacob Carlborg via Digitalmars-d digitalmars-d at puremagic.com
Thu Jun 22 01:16:51 PDT 2017


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. The call to dlsym(), of
> course, simply casts the return value to FuncImpl, because dlsym()
> doesn't know anything about what the symbol's type might be, and just
> returns void*:
> 
> 	FuncImpl impl = cast(FuncImpl) dlsym(lib, symbol);

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.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list