| Thread overview | ||||||||
|---|---|---|---|---|---|---|---|---|
|
April 15, 2014 Extern Keyword for Function Type | ||||
|---|---|---|---|---|
| ||||
exten(C) {
testFunction(int function(int));
}
testFunction now requires an external function as parameter. It can't be called with a pointer to a D function.
Logical Solution:
extern(C) {
testFunction(extern(D) int function(int)); // DOES NOT COMPILE
}
How do you fix this without moving it outside the global extern block?
| ||||
April 15, 2014 Re: Extern Keyword for Function Type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On Tuesday, 15 April 2014 at 20:15:42 UTC, Jeroen Bollen wrote:
> exten(C) {
> testFunction(int function(int));
> }
>
> testFunction now requires an external function as parameter. It can't be called with a pointer to a D function.
>
> Logical Solution:
>
> extern(C) {
> testFunction(extern(D) int function(int)); // DOES NOT COMPILE
> }
>
> How do you fix this without moving it outside the global extern block?
Whoops, assume testFunction is defined as "void testFunction(...)"
| |||
April 15, 2014 Re: Extern Keyword for Function Type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | C has no knowledge of D ABI so this can't work. If you just want to store D function pointer to later retrieve it and call from D code, you can as well store it as void* (or extern(C) with similar signature to preserve part of type) and cast upon interfacing. | |||
April 15, 2014 Re: Extern Keyword for Function Type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Dicebot | On Tuesday, 15 April 2014 at 20:19:36 UTC, Dicebot wrote:
> C has no knowledge of D ABI so this can't work. If you just want to store D function pointer to later retrieve it and call from D code, you can as well store it as void* (or extern(C) with similar signature to preserve part of type) and cast upon interfacing.
Hmm so the function passed should be declared as extern C but be defined nonetheless? That makes sense.
| |||
April 15, 2014 Re: Extern Keyword for Function Type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jeroen Bollen | On 04/15/14 22:15, Jeroen Bollen wrote:
> exten(C) {
> testFunction(int function(int));
> }
>
> testFunction now requires an external function as parameter. It can't be called with a pointer to a D function.
>
> Logical Solution:
>
> extern(C) {
> testFunction(extern(D) int function(int)); // DOES NOT COMPILE
> }
>
> How do you fix this without moving it outside the global extern block?
extern(C) {
extern (D) alias FP = int function(int);
void testFunction(FP);
}
artur
| |||
April 15, 2014 Re: Extern Keyword for Function Type | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Artur Skawina | On Tuesday, 15 April 2014 at 22:32:10 UTC, Artur Skawina wrote:
> extern(C) {
> extern (D) alias FP = int function(int);
> void testFunction(FP);
> }
>
> artur
Still does not allow you to actually call it from C side, so this is somewhat confusing.
I'd really just go with void*
| |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply