int C function
Christophe
travert at phare.normalesup.org
Fri Sep 30 18:27:43 PDT 2011
Ellery Newcomer , dans le message (digitalmars.D.learn:29885), a écrit :
> weird error. anyone know what's going on?
>
> [ellery at localhost d]$ cat test.d
> extern(C) int puts(const char *s);
> class X{
> @property void tt(int function(const char *) xz){
> }
> }
> void main(){
> X x = new X();
> x.tt = &puts;
> }
> [ellery at localhost d]$ dmd test
> test.d(8): Error: function test.X.tt (int function(const const(char*))
> xz) is not callable using argument types (int C function(const
> const(char*) s))
> test.d(8): Error: cannot implicitly convert expression (& puts) of type
> int C function(const const(char*) s) to int function(const const(char*))
The compiler is actually quite explicit: puts is a C function, and X.tt
expects a normal D function. You can solve your problem with a wrapper.
Try:
int cPuts(const char* s) { return puts(s); }
and then :
x.tt = &cPuts();
By the way, it is not very d-ish to use an "int function(const char*)".
we prefer: "int delegate(const char[])". And then:
| int cPuts(const char[] s) { return puts(toStringz(s)); }
But it depends on what you exactly are trying to do.
toStringz can be found in std.string.
--
Christophe
More information about the Digitalmars-d-learn
mailing list