Re: Function Pointer

2023-08-31 Thread vino via Digitalmars-d-learn
Vino ``` To get a function pointer type from a function type, you can add `*` on the end: void func(int) {} alias FuncType = typeof(func); pragma(msg, FuncType); // void(int) alias FuncPtrType = FuncType*; pragma(msg, FuncPtrType); // void function(int) static asser

Re: Function Pointer

2023-08-30 Thread Paul Backus via Digitalmars-d-learn
On Wednesday, 30 August 2023 at 17:48:19 UTC, Vino wrote: Hi All, Request your help on hot to create a pointer for a function whose function type is Result. ``` ReturnType!(typeof(&test)).stringof; // Result From Vino ``` To get a function pointer type from a function type, you can

Re: How can a function pointer required to be extern(C)?

2023-04-13 Thread rempas via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 21:00:04 UTC, John Chapman wrote: You can also express it like this: ```d extern(C) alias FuncPtr = void* function(void*); ``` Thank you! This is how I was planning to do anyway because other that the fact that I like the syntax of that a little bit more, this

Re: How can a function pointer required to be extern(C)?

2023-04-13 Thread rempas via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 20:36:59 UTC, H. S. Teoh wrote: IMO this is a bug either in D's syntax or in the parser. I'd file an enhancement request. In the meantime, you can use alias as a workaround: ---snip--- extern(C) void* abc(void*) {return null;} alias FuncPtr = typeof(

Re: How can a function pointer required to be extern(C)?

2023-04-12 Thread John Chapman via Digitalmars-d-learn
On Wednesday, 12 April 2023 at 20:36:59 UTC, H. S. Teoh wrote: ---snip--- extern(C) void* abc(void*) {return null;} alias FuncPtr = typeof(&abc); You can also express it like this: ```d extern(C) alias FuncPtr = void* function(void*); ```

Re: How can a function pointer required to be extern(C)?

2023-04-12 Thread H. S. Teoh via Digitalmars-d-learn
{ return this.thread_id; } > } > > ``` > > Yes, I'm trying to "encapsulate" the Pthread (POSIX threads) API. > Normally, the function pointer that is passed to "pthread_create" must > be "extern(C)" and this is the complaining that the compile d

How can a function pointer required to be extern(C)?

2023-04-12 Thread rempas via Digitalmars-d-learn
const(pthread_attr_t*) attr = null) { pthread_create(&this.thread_id, attr, func, arg); } @property: pthread_t id() { return this.thread_id; } } ``` Yes, I'm trying to "encapsulate" the Pthread (POSIX threads) API. Normally, the function pointer that is passed to &

Re: Can't assign extern(C) function pointer to D variable?

2022-11-22 Thread XavierAP via Digitalmars-d-learn
, and because it allows the remaining ScopeCleanup struct to be more general purpose, for non-C functions, and for functions that don't return void but an error code which I want to discard.) The first problem was indeed that a C function pointer "is not" a D one. So annotati

Re: Can't assign extern(C) function pointer to D variable?

2022-11-22 Thread ag0aep6g via Digitalmars-d-learn
On 22.11.22 22:11, XavierAP wrote: I was surprised when it didn't compile, though I immediately found it understandable... Already read through https://dlang.org/spec/interfaceToC.html and https://wiki.dlang.org/Bind_D_to_C Is it really the case (that an extern(C) function pointer cann

Re: Can't assign extern(C) function pointer to D variable?

2022-11-22 Thread Hipreme via Digitalmars-d-learn
On Tuesday, 22 November 2022 at 21:11:37 UTC, XavierAP wrote: I was surprised when it didn't compile, though I immediately found it understandable... Already read through https://dlang.org/spec/interfaceToC.html and https://wiki.dlang.org/Bind_D_to_C [...] You need to create an alias contain

Can't assign extern(C) function pointer to D variable?

2022-11-22 Thread XavierAP via Digitalmars-d-learn
I was surprised when it didn't compile, though I immediately found it understandable... Already read through https://dlang.org/spec/interfaceToC.html and https://wiki.dlang.org/Bind_D_to_C Is it really the case (that an extern(C) function pointer cannot be assigned to a D variable)? Or is

Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread kdevel via Digitalmars-d-learn
On Friday, 31 December 2021 at 12:36:46 UTC, H. S. Teoh wrote: ``` void lyr(alias Fn)(ref R r) { Fn(r); } ``` Thanks! That helped me reinvent the engine.

Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Dec 31, 2021 at 11:52:21AM +, kdevel via Digitalmars-d-learn wrote: [...] > That is what I want to do. The function template lyr shall be > (explicitly) instantiated in order to put the resulting function > pointer into an AA. The call signature of lyr!(foo) and foo must be &

Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread kdevel via Digitalmars-d-learn
function pointer into an AA. The call signature of lyr!(foo) and foo must be the same. In C++ this looks like this: ```C++ struct R { }; // typedef void (* Fn) (R &); // ptr version typedef void (& Fn) (R &); template static void lyr (R &r) { // invoke f } static void foo (R

Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread kdevel via Digitalmars-d-learn
On Friday, 31 December 2021 at 03:02:08 UTC, Tejas wrote: [...] Is it okay to use template parameter instead of **template value** parameter? ```d class R { } void foo (R r) { } void lyr (fp_type, R) (fp_type fp, R r) { } pragma (msg, typeof (&foo)); R r; void main(){ auto foo_ptr = &foo;

Re: function(pointer) as template argument, explicit template instantiation

2021-12-31 Thread data pulverizer via Digitalmars-d-learn
Pointers are runtime entities and are not suitable template parameters (compile time). So assuming that you are trying to either pass a function constant of a specific type signature as a template argument, or a function pointer as an argument with either a template specialisation or co

Re: function(pointer) as template argument, explicit template instantiation

2021-12-30 Thread Tejas via Digitalmars-d-learn
On Friday, 31 December 2021 at 00:57:26 UTC, kdevel wrote: ```dptr.d class R { } void foo (R r) { } alias fn = void function (R); void lyr (fn F) (R r) { } immutable fn foo_ptr = &foo; // line 14 pragma (msg, typeof (foo_ptr)); auto ptr = lyr!(foo_ptr);// line 17 ``` dmd reports: ``` im

function(pointer) as template argument, explicit template instantiation

2021-12-30 Thread kdevel via Digitalmars-d-learn
```dptr.d class R { } void foo (R r) { } alias fn = void function (R); void lyr (fn F) (R r) { } immutable fn foo_ptr = &foo; // line 14 pragma (msg, typeof (foo_ptr)); auto ptr = lyr!(foo_ptr);// line 17 ``` dmd reports: ``` immutable(void function(R)) dptr.d(14): Error: expression `& f

Re: How to do a function pointer to "malloc" and "free"?

2021-10-17 Thread Adam Ruppe via Digitalmars-d-learn
On Sunday, 17 October 2021 at 23:07:15 UTC, Elmar wrote: Do you have a link for more information how to initialize the D runtime? Export a function that calls this: http://druntime.dpldocs.info/core.runtime.Runtime.initialize.html And also export a function that calls this: http://druntime.dpl

Re: How to do a function pointer to "malloc" and "free"?

2021-10-17 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 17:14:30 UTC, Adam Ruppe wrote: On Sunday, 10 October 2021 at 13:52:57 UTC, Elmar wrote: The language subset "BetterC" is required for calling D functions from C though. This is false. You can use any D features when calling it from C, you just need to provide a

Re: How to do a function pointer to "malloc" and "free"?

2021-10-11 Thread Steven Schveighoffer via Digitalmars-d-learn
On 10/10/21 6:44 AM, rempas wrote: I'm having the following C code: ``` static void* (*ppmalloc)(size_t) = malloc; static void (*ppfree)(void*) = free; ``` I want to covert this code in D so I try to do the following: ``` static void* function(size_t)*ppmalloc = malloc; static void  function(v

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 14:00:37 UTC, Elmar wrote: On Sunday, 10 October 2021 at 13:56:06 UTC, rempas wrote: Actually I know about BetterC and how to call C functions from D and visa versa. I would also disagree that "BetterC" is almost no improvement over C as about 90% of the language

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Adam Ruppe via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:52:57 UTC, Elmar wrote: The language subset "BetterC" is required for calling D functions from C though. This is false. You can use any D features when calling it from C, you just need to provide an init and term function that is called from C that runtime in

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:56:06 UTC, rempas wrote: Actually I know about BetterC and how to call C functions from D and visa versa. I would also disagree that "BetterC" is almost no improvement over C as about 90% of the language is there!! C++ classes are also supported Nice :-) , y

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:52:57 UTC, Elmar wrote: The language subset "BetterC" is required for calling D functions from C though. Unfortunately, the runtime features of BetterC are limited and some of C's language features aren't availabe like C99 variable-length-arrays. "BetterC" is

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:52:57 UTC, Elmar wrote: Hopefully it will :-) . D has some good C support. You can call any C function from `D` by declaring it `extern(C) `. The language subset "BetterC" is required for calling D functions from C though. Unfortunately, the runtime features

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
On Sunday, 10 October 2021 at 13:10:27 UTC, rempas wrote: Thanks, I'm converting a library from C to D so I have to fix all the other bugs first to see If it's working but probably it will. Have an amazing day my friend! Hopefully it will :-) . D has some good C support. You can call any C

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread rempas via Digitalmars-d-learn
On Sunday, 10 October 2021 at 11:26:18 UTC, Elmar wrote: Hello rempas. This is the way: ```d import core.stdc.stdlib : malloc, free; extern(C) void* function(ulong) mallocPointer = &malloc; extern(C) void function(void*) freePointer = &free; ``` `function` in the type is already a

Re: How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread Elmar via Digitalmars-d-learn
oc, free; extern(C) void* function(ulong) mallocPointer = &malloc; extern(C) void function(void*) freePointer = &free; ``` `function` in the type is already a function pointer. Not immediately obvious though: You also must annotate the type with `extern(C)` otherwise it will not work.

How to do a function pointer to "malloc" and "free"?

2021-10-10 Thread rempas via Digitalmars-d-learn
I'm having the following C code: ``` static void* (*ppmalloc)(size_t) = malloc; static void (*ppfree)(void*) = free; ``` I want to covert this code in D so I try to do the following: ``` static void* function(size_t)*ppmalloc = malloc; static void function(void*)*ppfree = free; ``` If I do th

Re: Function Pointer Not Working

2020-11-19 Thread Marcone via Digitalmars-d-learn
I will wait with this code. WaitForSingleObject(threading, INFINITE);

Re: Function Pointer Not Working

2020-11-19 Thread Marcone via Digitalmars-d-learn
On Thursday, 19 November 2020 at 15:51:09 UTC, Kagamin wrote: The delegate is stored on the stack of the calling thread, the created thread loads it from there, but the calling thread doesn't wait for that and clobbers the stack right away. If you were lucky your code would crash. The thread

Re: Function Pointer Not Working

2020-11-19 Thread Kagamin via Digitalmars-d-learn
The delegate is stored on the stack of the calling thread, the created thread loads it from there, but the calling thread doesn't wait for that and clobbers the stack right away. If you were lucky your code would crash.

Re: Function Pointer Not Working

2020-11-19 Thread Marcone via Digitalmars-d-learn
Solved replacing this line: CreateThread(null, 0, &_fun, &fun, 0, null); to this code: task!({CreateThread(null, 0, &_fun, &fun, 0, null);}).executeInNewThread();

Re: Function Pointer Not Working

2020-11-18 Thread Vladimir Panteleev via Digitalmars-d-learn
))(); // Do not show "Hello World!" :( return 0; } CreateThread(null, 0, &_fun, &fun, 0, null); } catch(Throwable){} } void main(){ null.threadingw({writeln("Hello World!");}); } A delegate

Function Pointer Not Working

2020-11-18 Thread Marcone via Digitalmars-d-learn
// Function threadingw() void threadingw(HWND hwn, void delegate() fun) nothrow { try { // Function _fun() extern(Windows) uint _fun(void * arg){ (*(cast(void delegate()*) arg))(); // Do not show "Hello World!" :(

Re: is function pointer least significant bit always zero ?

2018-10-27 Thread Ali Çehreli via Digitalmars-d-learn
On 10/27/2018 09:16 PM, learnfirst1 wrote: I plan to use function pointer least significant bit to store some information. If there is no GC on my system,  I think it will help the memory is well aligned. The question is all the function least significant bit is zero ? Most definitely

is function pointer least significant bit always zero ?

2018-10-27 Thread learnfirst1 via Digitalmars-d-learn
I plan to use function pointer least significant bit to store some information. If there is no GC on my system, I think it will help the memory is well aligned. The question is all the function least significant bit is zero ?

Re: Checking if a function pointer is set or null

2018-04-08 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, April 09, 2018 00:25:08 solidstate1991 via Digitalmars-d-learn wrote: > Would the if(!(myFunctionPointer is null)){} work is I > intended? You can also do if(myFunctionPointer !is null) - Jonathan M Davis

Re: Checking if a function pointer is set or null

2018-04-08 Thread Uknown via Digitalmars-d-learn
On Monday, 9 April 2018 at 00:25:08 UTC, solidstate1991 wrote: Would the if(!(myFunctionPointer is null)){} work is I intended? Yes, that works as you expect https://run.dlang.io/is/ZTtm0P

Checking if a function pointer is set or null

2018-04-08 Thread solidstate1991 via Digitalmars-d-learn
Would the if(!(myFunctionPointer is null)){} work is I intended?

Re: Get a string of a function name from a function pointer?

2017-07-10 Thread Ali Çehreli via Digitalmars-d-learn
On 07/10/2017 05:26 AM, SauceKode wrote: > I need to pass a group of (C) function pointers to another language from > D... is there a way to derrive a name from a function pointer? Or do I > have to manually list out the names? libunwind should be able to provide that functionality.

Get a string of a function name from a function pointer?

2017-07-10 Thread SauceKode via Digitalmars-d-learn
I need to pass a group of (C) function pointers to another language from D... is there a way to derrive a name from a function pointer? Or do I have to manually list out the names?

Re: The reason for SIGSEGV function pointer problem

2017-06-08 Thread Russel Winder via Digitalmars-d-learn
Thanks also to Paolo Invernizzi and ag0aep6g for answering with a similar response. Using Mike's response as it has extra detail. On Wed, 2017-06-07 at 20:00 +0200, Mike Wey via Digitalmars-d-learn wrote: > On 06/07/2017 06:50 PM, Russel Winder via Digitalmars-d-learn wrote: > > So why isn't &chec

Re: The reason for SIGSEGV function pointer problem

2017-06-07 Thread Mike Wey via Digitalmars-d-learn
On 06/07/2017 06:50 PM, Russel Winder via Digitalmars-d-learn wrote: So why isn't &checkFrontend a thing of type check_frontend_t* AFAIK, you would usually translate: typedef int (check_frontend_t*)(void *args, struct dvb_v5_fe_parms *parms); into: alias check_frontend_t = extern(C) int fun

Re: The reason for SIGSEGV function pointer problem

2017-06-07 Thread ag0aep6g via Digitalmars-d-learn
On 06/07/2017 06:50 PM, Russel Winder via Digitalmars-d-learn wrote: So why isn't &checkFrontend a thing of type check_frontend_t*? It's a thing of type `check_frontend_t`, which is a function pointer already. When you add an asterisk, you get a pointer to a function pointer.

Re: The reason for SIGSEGV function pointer problem

2017-06-07 Thread Paolo Invernizzi via Digitalmars-d-learn
On Wednesday, 7 June 2017 at 16:50:26 UTC, Russel Winder wrote: In the constructor of an object to abstract the result of a call to the C library code, the parameter is: check_frontend_t* cf You should remove the pointer here... /Paolo

The reason for SIGSEGV function pointer problem

2017-06-07 Thread Russel Winder via Digitalmars-d-learn
OK, so I have narrowed down my SIGSEGV problem to having no real idea how to do C function pointers in D code. So I have a callback function that will be called from C library code. It currently has signature: extern(C) int checkFrontend(void* _arguments, dvb_v5_fe_parms* frontendParameters)

Re: How to overload member function pointer and a regualr member function

2017-04-26 Thread ParticlePeter via Digitalmars-d-learn
On Wednesday, 26 April 2017 at 08:24:08 UTC, Basile B. wrote: On Tuesday, 25 April 2017 at 18:58:58 UTC, Ali Çehreli wrote: On 04/25/2017 11:54 AM, Ali Çehreli wrote: My analysis is wrong because that writefln() is for the bar(float) overload but I still think what you want is achieved. Ali

Re: How to overload member function pointer and a regualr member function

2017-04-26 Thread Basile B. via Digitalmars-d-learn
On Tuesday, 25 April 2017 at 18:58:58 UTC, Ali Çehreli wrote: On 04/25/2017 11:54 AM, Ali Çehreli wrote: My analysis is wrong because that writefln() is for the bar(float) overload but I still think what you want is achieved. Ali No it's ok, it works. The additional indirection is well avoid

Re: How to overload member function pointer and a regualr member function

2017-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 04/25/2017 11:54 AM, Ali Çehreli wrote: > _Dmain: > pushRBP > movRBP,RSP > subRSP,010h > movRAX,_D6deneme4funcFifZv@GOTPCREL[RIP] > mov-010h[RBP],RAX > movssXMM0,FLAT:.rodata[00h][RIP] > movss-8[RBP],XMM0 >

Re: How to overload member function pointer and a regualr member function

2017-04-25 Thread Ali Çehreli via Digitalmars-d-learn
On 04/25/2017 11:28 AM, ParticlePeter wrote: > On Tuesday, 25 April 2017 at 16:27:43 UTC, Basile B. wrote: >> with pragma(inline, true), the function body should be injected at the >> call sites. > > This would not help I fear, the body of the function pointer is unknown &g

Re: How to overload member function pointer and a regualr member function

2017-04-25 Thread ParticlePeter via Digitalmars-d-learn
hat I would like to avoid, the additional indirection to call the function pointer with the original argument count. Oops, i can believe i didn't read the last part of your question. Do you have any idea about the likelihood of the compiler removing this indirection as an optimizations? w

Re: How to overload member function pointer and a regualr member function

2017-04-25 Thread Basile B. via Digitalmars-d-learn
l the function pointer with the original argument count. Oops, i can believe i didn't read the last part of your question. Do you have any idea about the likelihood of the compiler removing this indirection as an optimizations? with pragma(inline, true), the function body should be inject

Re: How to overload member function pointer and a regualr member function

2017-04-25 Thread ParticlePeter via Digitalmars-d-learn
on to call the function pointer with the original argument count. Do you have any idea about the likelihood of the compiler removing this indirection as an optimizations?

Re: How to overload member function pointer and a regualr member function

2017-04-25 Thread Basile B. via Digitalmars-d-learn
On Monday, 24 April 2017 at 16:46:21 UTC, ParticlePeter wrote: I would like to have this kind of struct: struct Foo { private int i; void function( int i, float f ) bar; // will be defined at runtime void bar( float f ) { bar( i, f ); } } [...] How else can I get the required beh

How to overload member function pointer and a regualr member function

2017-04-24 Thread ParticlePeter via Digitalmars-d-learn
I would like to have this kind of struct: struct Foo { private int i; void function( int i, float f ) bar; // will be defined at runtime void bar( float f ) { bar( i, f ); } } But apparently the function pointer and the member function cannot have the same name: Error: function

Re: Function pointer pitfalls

2017-03-14 Thread Inquie via Digitalmars-d-learn
On Tuesday, 14 March 2017 at 19:14:34 UTC, H. S. Teoh wrote: On Tue, Mar 14, 2017 at 06:59:58PM +, Inquie via Digitalmars-d-learn wrote: [...] [...] > [...] [...] [...] [...] Keep in mind, though, that the above creates a function pointer with the same signature as the mem

Re: Function pointer pitfalls

2017-03-14 Thread H. S. Teoh via Digitalmars-d-learn
) membptr; > > pragma(msg, typeof(membptr)); // prints `int function(float x)` > > > > If you need to refer to the function pointer type frequently, you > > could alias it to something easier to type; > > > > alias FuncPtr = typeof(&X.method); > >

Re: Function pointer pitfalls

2017-03-14 Thread Inquie via Digitalmars-d-learn
function that could use any types. Is there any pitfalls like there are in C++ from generating a function pointer from them? e.g., X foo(A,B,C) @R @S @T -> X function(A,B,C) @R @S @T fooptr; In my case, there are no attributes, so that might ease the burden. e.g., a template that convert

Re: Function pointer pitfalls

2017-03-14 Thread H. S. Teoh via Digitalmars-d-learn
re any pitfalls like there are in C++ from generating a function > pointer from them? > > e.g., > > X foo(A,B,C) @R @S @T -> X function(A,B,C) @R @S @T fooptr; > > In my case, there are no attributes, so that might ease the burden. > > e.g., a templat

Function pointer pitfalls

2017-03-14 Thread Inquie via Digitalmars-d-learn
I am generating member function pointers using the declaration specified from a standard member function. The standard member function is a valid D function that could use any types. Is there any pitfalls like there are in C++ from generating a function pointer from them? e.g., X foo(A,B,C

Re: How can I implement this in D: a variant array of varying function pointer types (diff number of args or types)

2017-01-17 Thread Nicholas Wilson via Digitalmars-d-learn
On Tuesday, 17 January 2017 at 10:49:14 UTC, Enjoys Math wrote: Z add(Z...)(Z a...) { return a + b; } func[] operatorPool = [&add!int]; Variant library isn't liking that. Removing & causes another error. Essentially I want a pool of all operators that I define, but these operators

Re: How can I implement this in D: a variant array of varying function pointer types (diff number of args or types)

2017-01-17 Thread Enjoys Math via Digitalmars-d-learn
On Tuesday, 17 January 2017 at 10:49:14 UTC, Enjoys Math wrote: Z add(Z...)(Z a...) { return a + b; } func[] operatorPool = [&add!int]; Variant library isn't liking that. Removing & causes another error. Essentially I want a pool of all operators that I define, but these operators

How can I implement this in D: a variant array of varying function pointer types (diff number of args or types)

2017-01-17 Thread Enjoys Math via Digitalmars-d-learn
Z add(Z...)(Z a...) { return a + b; } func[] operatorPool = [&add!int]; Variant library isn't liking that. Removing & causes another error. Essentially I want a pool of all operators that I define, but these operators can be of differing types (which I should be able to programatic

Re: Cryptic C function pointer for conversion

2016-12-17 Thread bachmeier via Digitalmars-d-learn
On Saturday, 17 December 2016 at 15:15:26 UTC, data pulverizer wrote: Does this mean that you can translate C code to D natively? I am currently only aware of the dstep package. It may not help you, but something I've done in the past is use Swig to create a Common Lisp interface. It transla

Re: Cryptic C function pointer for conversion

2016-12-17 Thread ketmar via Digitalmars-d-learn
p.s.: that means that i didn't really *decoded* that declaration, just brute-forced someting that c++ compiler happily accepts. so take it with a grain of salt. ;-)

Re: Cryptic C function pointer for conversion

2016-12-17 Thread data pulverizer via Digitalmars-d-learn
fed to C(++) compiler and translate to D. p.s. I confirmed your interpretation on stackoverflow: http://stackoverflow.com/questions/8722817/syntax-for-a-pointer-to-a-function-returning-a-function-pointer-in-c

Re: Cryptic C function pointer for conversion

2016-12-17 Thread ketmar via Digitalmars-d-learn
On Saturday, 17 December 2016 at 15:15:26 UTC, data pulverizer wrote: Does this mean that you can translate C code to D natively? I am currently only aware of the dstep package. with my head and bare hands. well, armed with some regular expressions. did you seen some of my "port" announcemen

Re: Cryptic C function pointer for conversion

2016-12-17 Thread data pulverizer via Digitalmars-d-learn
char *zSymbol); ... } Thanks ketmar, I guess that this means I got it the other way round the function pointer that is returned is the function that takes in and returns void. at least that is what i managed to decode, fed to C(++) compiler and translate to D. Does this mean that you can

Re: Cryptic C function pointer for conversion

2016-12-17 Thread ketmar via Digitalmars-d-learn
On Saturday, 17 December 2016 at 13:39:27 UTC, data pulverizer wrote: that is what it means, in D: //void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); struct sqlite3_vfs {} extern(C) { alias RetRes = void function (); alias DeclType = RetRes function (sqlite3_vfs *a,void *b,

Cryptic C function pointer for conversion

2016-12-17 Thread data pulverizer via Digitalmars-d-learn
I have come across a function pointer in C that I am attempting to convert, and am not sure what the current interpretation is: ``` \\ The C Code: void (*(*xDlSym)(sqlite3_vfs*,void*, const char *zSymbol))(void); ``` The best I can tell is that this is a function pointer that returns a

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread jmh530 via Digitalmars-d-learn
On Friday, 19 February 2016 at 22:34:48 UTC, Chris Wright wrote: I tested this a fair bit today, and I haven't been able to do any of the nefarious things I expected to be able to do. No overwriting variables in the caller's scope, no smashing stack pointers, etc. I was surprised by this re

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread Chris Wright via Digitalmars-d-learn
On Fri, 19 Feb 2016 21:57:46 +, Yuxuan Shui wrote: > I don't think it's safe to convert between function pointer with > different number of arguments... It's possible to mess up the stack > frame. I tested this a fair bit today, and I haven't been able to do a

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread jmh530 via Digitalmars-d-learn
On Friday, 19 February 2016 at 22:07:25 UTC, Chris Wright wrote: If you want to cast function pointers successfully, you have to know the D calling convention. [snip] I figured there was an explanation. Definitely "here be dragons" territory. I hope I can figure out a better solution, but

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread Chris Wright via Digitalmars-d-learn
On Fri, 19 Feb 2016 20:45:23 +, jmh530 wrote: > I tried to use a cast (below) to modify the function pointer, but it is > printing the second number instead of the first. I find this behavior > strange... If you want to cast function pointers successfully, you have to know the

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread Yuxuan Shui via Digitalmars-d-learn
int y). I don't think I had looked at what you had done carefully enough. Basically, you just define a new function and take a function pointer of that. That might be a brute force solution. I tried to use a cast (below) to modify the function pointer, but it is printing the second number

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread jmh530 via Digitalmars-d-learn
carefully enough. Basically, you just define a new function and take a function pointer of that. That might be a brute force solution. I tried to use a cast (below) to modify the function pointer, but it is printing the second number instead of the first. I find this behavior strange... int

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread jmh530 via Digitalmars-d-learn
On Friday, 19 February 2016 at 14:21:26 UTC, Kagamin wrote: int bar(int x) { return x; } int baz(int x, int y) { return bar(x); } void main() { import std.stdio : writeln; int function(int x, int y) foo_bar = &baz; writeln(foo_bar(1, 2)); } T

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread jmh530 via Digitalmars-d-learn
On Friday, 19 February 2016 at 11:26:56 UTC, Nicholas Wilson wrote: Like alias fp1 = int function(int x); alias fp2 = int function(int x, int y); auto foo(T)(T f) { static if (is(T == fp2)) return f; else static if (is(T == fp1)) { return

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread Kagamin via Digitalmars-d-learn
On Friday, 19 February 2016 at 05:41:01 UTC, jmh530 wrote: void main() { import std.stdio : writeln; auto foo_bar = foo(&bar); writeln(qux(1, 2, foo_bar)); //compiler error writeln(qux(1, 2, &baz)); } int bar(int x) { return x; } int baz(int x,

Re: Modify Function Pointer to Take Additional Parameters

2016-02-19 Thread Nicholas Wilson via Digitalmars-d-learn
On Friday, 19 February 2016 at 05:41:01 UTC, jmh530 wrote: I'm trying to write a function that will adjust the parameters of a function pointer. I think the problem is that it defaults to a delegate not that it cannot be one does clarifying this to the compiler work Like alias fp1

Modify Function Pointer to Take Additional Parameters

2016-02-18 Thread jmh530 via Digitalmars-d-learn
I'm trying to write a function that will adjust the parameters of a function pointer. In the code below, my goal is to call the function qux with a variety of different function pointers (in the actual application, I don't have the ability to modify qux). I created a function

Re: LNK2019 error from using a function pointer to core.bitop functions?

2015-07-17 Thread Matthew Gamble via Digitalmars-d-learn
On Friday, 17 July 2015 at 15:49:46 UTC, Roland Hadinger wrote: On Friday, 17 July 2015 at 15:47:39 UTC, Roland Hadinger wrote: Otherwise, I'd use templates and an alias. Maybe this will result in faster code: bool opIndexAssign(bool value, size_t[2] inds) { void impl(bool b)(s

Re: LNK2019 error from using a function pointer to core.bitop functions?

2015-07-17 Thread Roland Hadinger via Digitalmars-d-learn
On Thursday, 16 July 2015 at 03:24:54 UTC, Matthew Gamble wrote: This member function of my struct uses a function pointer btx. When the line declaring the function pointer is present I get a LNK2019 error: unresolved external symbol. Just guessing, probably because bts and btr are intrinsics

Re: LNK2019 error from using a function pointer to core.bitop functions?

2015-07-17 Thread Roland Hadinger via Digitalmars-d-learn
On Friday, 17 July 2015 at 15:47:39 UTC, Roland Hadinger wrote: Otherwise, I'd use templates and an alias. Maybe this will result in faster code: bool opIndexAssign(bool value, size_t[2] inds) { void impl(bool b)(size_t[2] inds) { static if(b)

LNK2019 error from using a function pointer to core.bitop functions?

2015-07-15 Thread Matthew Gamble via Digitalmars-d-learn
This member function of my struct uses a function pointer btx. When the line declaring the function pointer is present I get a LNK2019 error: unresolved external symbol. bool opIndexAssign(bool value, size_t[2] inds) { int function(size_t*, size_t) btx = (value) ? &bts : &btr; //

Re: Function pointer array slice?

2015-07-11 Thread ketmar via Digitalmars-d-learn
On Sat, 11 Jul 2015 11:37:03 +, Tofu Ninja wrote: >> void function() nothrow pure @nogc @safe [2]arrayName; >> >> is perfectly fine too. > > Ahh, guess that makes sense, I kept trying to put the [] over near > function()... attributes are the parts of the type. and the rule is really simpl

Re: Function pointer array slice?

2015-07-11 Thread Tofu Ninja via Digitalmars-d-learn
On Saturday, 11 July 2015 at 10:54:45 UTC, ketmar wrote: On Sat, 11 Jul 2015 09:54:40 +, tcak wrote: On Saturday, 11 July 2015 at 09:30:43 UTC, Tofu Ninja wrote: So simple syntax question, how do I make an array slice of function pointers? I just have no idea where to put the [] on somet

Re: Function pointer array slice?

2015-07-11 Thread ketmar via Digitalmars-d-learn
On Sat, 11 Jul 2015 09:54:40 +, tcak wrote: > On Saturday, 11 July 2015 at 09:30:43 UTC, Tofu Ninja wrote: >> So simple syntax question, how do I make an array slice of function >> pointers? >> >> I just have no idea where to put the [] on something like >> >> void function() nothrow pure

Re: Function pointer array slice?

2015-07-11 Thread tcak via Digitalmars-d-learn
On Saturday, 11 July 2015 at 09:30:43 UTC, Tofu Ninja wrote: So simple syntax question, how do I make an array slice of function pointers? I just have no idea where to put the [] on something like void function() nothrow pure @nogc @safe arrayName; Or should I just alias it and make an a

Function pointer array slice?

2015-07-11 Thread Tofu Ninja via Digitalmars-d-learn
So simple syntax question, how do I make an array slice of function pointers? I just have no idea where to put the [] on something like void function() nothrow pure @nogc @safe arrayName; Or should I just alias it and make an array of the alias? alias f = void function() nothrow pur

Re: Function name from function pointer

2015-04-11 Thread Paul D Anderson via Digitalmars-d-learn
On Saturday, 11 April 2015 at 19:08:50 UTC, Marco Leise wrote: Am Sat, 11 Apr 2015 18:28:35 + schrieb "Paul D Anderson" : Is there a way to return the name of a function (a string) from a pointer to that function? Function pointer example from D Reference: --- int function()

Re: Function name from function pointer

2015-04-11 Thread bearophile via Digitalmars-d-learn
Paul D Anderson: Is there a way to return the name of a function (a string) from a pointer to that function? Perhaps creating a string[void*] AA and initializing with all the function pointers you care about. Bye, bearophile

Re: Function name from function pointer

2015-04-11 Thread Marco Leise via Digitalmars-d-learn
Am Sat, 11 Apr 2015 18:28:35 + schrieb "Paul D Anderson" : > Is there a way to return the name of a function (a string) from a > pointer to that function? > > Function pointer example from D Reference: > --- > int function() fp; > > void test() > { &

Function name from function pointer

2015-04-11 Thread Paul D Anderson via Digitalmars-d-learn
Is there a way to return the name of a function (a string) from a pointer to that function? Function pointer example from D Reference: --- int function() fp; void test() { static int a = 7; static int foo() { return a + 3; } fp = &foo; } void bar() { test(); int i

Re: Function pointer to member function.

2013-10-17 Thread TheFlyingFiddle
On Thursday, 17 October 2013 at 03:21:38 UTC, Chris Cain wrote: On Thursday, 17 October 2013 at 01:17:21 UTC, TheFlyingFiddle wrote: I would like to get access to a member function pointer. Taking the this* as the first argument. ...snip... How should i implement getFP above? Is it even

Re: Function pointer to member function.

2013-10-16 Thread Chris Cain
On Thursday, 17 October 2013 at 01:17:21 UTC, TheFlyingFiddle wrote: I would like to get access to a member function pointer. Taking the this* as the first argument. ...snip... How should i implement getFP above? Is it even possible? Well, it's certainly possible. If you were to do

Function pointer to member function.

2013-10-16 Thread TheFlyingFiddle
I would like to get access to a member function pointer. Taking the this* as the first argument. class Foo { void bar(int a) { //do something awesome } } unittest { Foo a = new Foo(); Foo b = new Foo(); auto fp = getFP!(Foo.bar); fp(a, 1); //Basically calls

Re: Call a function with a function pointer

2013-10-13 Thread Benjamin Thaut
Am 13.10.2013 17:17, schrieb Artur Skawina: On 10/13/13 16:43, Benjamin Thaut wrote: Am 10.10.2013 17:45, schrieb Namespace: 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 print

  1   2   >