Thread overview | ||||||||
---|---|---|---|---|---|---|---|---|
|
August 22, 2013 alias and extern(C) for callbacks | ||||
---|---|---|---|---|
| ||||
The D language page on interfacing to C http://dlang.org/interfaceToC.html says under 'Callbacks' that e.g. alias extern(C) int function(int, int) Callback; // D code is OK, so I am wondering if I can factor out the extern(C) from aliases as follows extern(C) { alias int function(int, int) Callback; // more aliases } Is Callback here a name for the same type as if it was defined by the following? alias extern(C) int function(int, int) Callback; |
August 23, 2013 Re: alias and extern(C) for callbacks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Carl Sturtivant | On Thursday, 22 August 2013 at 23:55:44 UTC, Carl Sturtivant wrote:
> Is Callback here a name for the same type as if it was defined by the following?
>
> alias extern(C) int function(int, int) Callback;
Yes. If you want to make sure, you can use a trait:
import std.traits;
extern(C) { void test1() { } }
void test2() { }
static assert(functionLinkage!test1 == "C");
static assert(functionLinkage!test2 != "C"); // D calling convention
|
August 23, 2013 Re: alias and extern(C) for callbacks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On Friday, 23 August 2013 at 00:00:13 UTC, Andrej Mitrovic wrote:
> On Thursday, 22 August 2013 at 23:55:44 UTC, Carl Sturtivant wrote:
>> Is Callback here a name for the same type as if it was defined by the following?
>>
>> alias extern(C) int function(int, int) Callback;
>
> Yes. If you want to make sure, you can use a trait:
>
> import std.traits;
>
> extern(C) { void test1() { } }
> void test2() { }
>
> static assert(functionLinkage!test1 == "C");
> static assert(functionLinkage!test2 != "C"); // D calling convention
Nice! Thank you.
|
August 23, 2013 Re: alias and extern(C) for callbacks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 2013-08-23 02:00, Andrej Mitrovic wrote: > static assert(functionLinkage!test2 != "C"); // D calling convention Or Windows, Pascal or C++. -- /Jacob Carlborg |
August 23, 2013 Re: alias and extern(C) for callbacks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jacob Carlborg | On 8/23/13, Jacob Carlborg <doob@me.com> wrote:
> On 2013-08-23 02:00, Andrej Mitrovic wrote:
>
>> static assert(functionLinkage!test2 != "C"); // D calling convention
>
> Or Windows, Pascal or C++.
I'd prefer if functionLinkage returned an enum, it's too easy to write "c" instead of "C" in some generic code and have a static if branch not taken.
|
August 23, 2013 Re: alias and extern(C) for callbacks | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrej Mitrovic | On 2013-08-23 13:10, Andrej Mitrovic wrote: > I'd prefer if functionLinkage returned an enum, it's too easy to write > "c" instead of "C" in some generic code and have a static if branch > not taken. Yeah, that's a good point. -- /Jacob Carlborg |
Copyright © 1999-2021 by the D Language Foundation