Using delegates in callbacks for extern(C) functions

simendsjo simendsjo at gmail.com
Sat Feb 25 10:41:30 PST 2012


On Sat, 25 Feb 2012 17:12:50 +0100, Timon Gehr <timon.gehr at gmx.ch> wrote:

> On 02/24/2012 07:22 PM, simendsjo wrote:
>> I have a C function taking a callback function as a parameter. My
>> thought was to wrap this up using a template, but I cannot get it to  
>> work:
>>
>> extern(C) alias void function() Callback;
>>
>> template Wrap(alias dg)
>> {
>> extern(C) void Wrap()
>> {
>> dg();
>> }
>> }
>>
>> void main()
>> {
>> Callback cb = &Wrap!( () {} );
>> }
>>
>> Error: cannot implicitly convert expression (&Wrap) of type void
>> delegate() pure nothrow @safe to extern (C) void function()
>
> This is a bug. '() {}' should be inferred as void function()pure nothrow  
> @safe. Workaround: The code compiles if you use function() {} instead. I  
> don't know if a bug report exists already.

Thanks. http://d.puremagic.com/issues/show_bug.cgi?id=7585


More information about the Digitalmars-d-learn mailing list