Jump to page: 1 2
Thread overview
Call a function with a function pointer
Oct 10, 2013
Namespace
Oct 10, 2013
Benjamin Thaut
Oct 10, 2013
Namespace
Oct 10, 2013
bearophile
Oct 10, 2013
Namespace
Oct 10, 2013
bearophile
Oct 10, 2013
Andrej Mitrovic
Oct 10, 2013
bearophile
Oct 13, 2013
Benjamin Thaut
Oct 13, 2013
Artur Skawina
Oct 13, 2013
Benjamin Thaut
Oct 10, 2013
Dicebot
Oct 10, 2013
Dicebot
Oct 10, 2013
Namespace
Oct 10, 2013
Dicebot
Oct 10, 2013
Namespace
Oct 10, 2013
Namespace
Oct 10, 2013
Dicebot
Oct 11, 2013
Artur Skawina
Oct 11, 2013
Dicebot
October 10, 2013
I have this function:
----
void foo(T)(void function(T*) test) { }
----

And want to call it with a C function:
----
foo!(SDL_Surface)(SDL_FreeSurface);
----

but I get:
Fehler	1	Error: foo (void function(SDL_Surface*) test) is not callable using argument types (extern (C) void function(SDL_Surface*) nothrow)

What would be the smartest solution?
October 10, 2013
Am 10.10.2013 16:13, schrieb Namespace:
> I have this function:
> ----
> void foo(T)(void function(T*) test) { }
> ----
>
> And want to call it with a C function:
> ----
> foo!(SDL_Surface)(SDL_FreeSurface);
> ----
>
> but I get:
> Fehler    1    Error: foo (void function(SDL_Surface*) test) is not
> callable using argument types (extern (C) void function(SDL_Surface*)
> nothrow)
>
> What would be the smartest solution?

If you can change the signature of foo just add a extern(c) to the function pointer declaration.

Otherwise just wrap the SDL_FreeSurface call into a delegate on the caller side.

-- 
Kind Regards
Benjamin Thaut
October 10, 2013
On Thursday, 10 October 2013 at 14:13:47 UTC, Namespace wrote:
> I have this function:
> ----
> void foo(T)(void function(T*) test) { }
> ----
>
> And want to call it with a C function:
> ----
> foo!(SDL_Surface)(SDL_FreeSurface);
> ----
>
> but I get:
> Fehler	1	Error: foo (void function(SDL_Surface*) test) is not callable using argument types (extern (C) void function(SDL_Surface*) nothrow)
>
> What would be the smartest solution?

Wrap it in a lambda. Or change foo() signature to accept `extern(C)` functions - you can't just mix calling convention.
October 10, 2013
On Thursday, 10 October 2013 at 14:26:37 UTC, Benjamin Thaut wrote:
> Am 10.10.2013 16:13, schrieb Namespace:
>> I have this function:
>> ----
>> void foo(T)(void function(T*) test) { }
>> ----
>>
>> And want to call it with a C function:
>> ----
>> foo!(SDL_Surface)(SDL_FreeSurface);
>> ----
>>
>> but I get:
>> Fehler    1    Error: foo (void function(SDL_Surface*) test) is not
>> callable using argument types (extern (C) void function(SDL_Surface*)
>> nothrow)
>>
>> What would be the smartest solution?
>
> If you can change the signature of foo just add a extern(c) to the function pointer declaration.
>
> Otherwise just wrap the SDL_FreeSurface call into a delegate on the caller side.

You mean like this?
----
void foo(T)(extern(C) void function(T*) func) {
		
}
----

That prints: Error: basic type expected, not extern	
October 10, 2013
On Thursday, 10 October 2013 at 14:28:20 UTC, Dicebot wrote:
> On Thursday, 10 October 2013 at 14:13:47 UTC, Namespace wrote:
>> I have this function:
>> ----
>> void foo(T)(void function(T*) test) { }
>> ----
>>
>> And want to call it with a C function:
>> ----
>> foo!(SDL_Surface)(SDL_FreeSurface);
>> ----
>>
>> but I get:
>> Fehler	1	Error: foo (void function(SDL_Surface*) test) is not callable using argument types (extern (C) void function(SDL_Surface*) nothrow)
>>
>> What would be the smartest solution?
>
> Wrap it in a lambda.

Example? I do not use lambdas often.
October 10, 2013
On Thursday, 10 October 2013 at 14:40:09 UTC, Namespace wrote:
> Example? I do not use lambdas often.

void foo(T)(void function(T*) test)
{
}

extern(C) void bar(int*) { }

void main()
{
	foo( (int* a) => bar(a) );
}

I don't know to what extent IFTI can work here though.
October 10, 2013
Namespace:

> You mean like this?
> ----
> void foo(T)(extern(C) void function(T*) func) {
> 		
> }
> ----
>
> That prints: Error: basic type expected, not extern	

In theory that's correct, in practice the compiler refuses that, it's in Bugzilla, so try to define the type outside the signature (untested):

alias TF = extern(C) void function(T*);

void foo(T)(TF func) {}

Bye,
bearophile
October 10, 2013
On Thursday, 10 October 2013 at 15:15:45 UTC, bearophile wrote:
> Namespace:
>
>> You mean like this?
>> ----
>> void foo(T)(extern(C) void function(T*) func) {
>> 		
>> }
>> ----
>>
>> That prints: Error: basic type expected, not extern	
>
> In theory that's correct, in practice the compiler refuses that, it's in Bugzilla, so try to define the type outside the signature (untested):
>
> alias TF = extern(C) void function(T*);
>
> void foo(T)(TF func) {}
>
> Bye,
> bearophile

/d917/f732.d(8): Error: basic type expected, not extern
/d917/f732.d(8): Error: semicolon expected to close alias declaration
/d917/f732.d(8): Error: no identifier for declarator void function(T*)
October 10, 2013
On Thursday, 10 October 2013 at 14:44:00 UTC, Dicebot wrote:
> On Thursday, 10 October 2013 at 14:40:09 UTC, Namespace wrote:
>> Example? I do not use lambdas often.
>
> void foo(T)(void function(T*) test)
> {
> }
>
> extern(C) void bar(int*) { }
>
> void main()
> {
> 	foo( (int* a) => bar(a) );
> }
>
> I don't know to what extent IFTI can work here though.

That works. Thanks.
October 10, 2013
Namespace:

> /d917/f732.d(8): Error: basic type expected, not extern
> /d917/f732.d(8): Error: semicolon expected to close alias declaration
> /d917/f732.d(8): Error: no identifier for declarator void function(T*)

It seems that even the new alias syntax doesn't support the extern :-) Perhaps this bug is not yet in Bugzilla.

Try:

alias extern(C) void function(T*) TF;
void foo(T)(TF func) {}

Bye,
bearophile
« First   ‹ Prev
1 2