Understanding Safety of Function Pointers vs. Addresses of Functions

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Jul 8 11:31:02 PDT 2015


On 7/8/15 11:49 AM, jmh530 wrote:
> On Wednesday, 8 July 2015 at 14:37:23 UTC, jmh530 wrote:
>> Thanks. I was worried I was doing something wrong.
>
> It seems like if you wrap the intrinsic function in another function
> than it works fine (below). Easy enough work-around, I suppose.
>
> If there is no intention to fix the pointer to assembly function issue
> (I have zero clue how to go about it, maybe there is a good reason it
> can't be fixed), this work-around could be implemented in the std.math
> library for those functions. I.e., put the assembly code in separate
> functions and then have the existing intrinsic functions act as wrappers
> to those.
>
> import std.math : cos;
>
> real cos_wrap(real x) pure nothrow @nogc @safe
> {
>      return cos(x);
> }
>
> void main()
> {
>      auto fp = &cos_wrap;
>      real x = 0;
>      real y = fp(x);
> }

You can use a function lambda:

auto fp = (real a) => cos(a);

Note, I had to put (real a) even though I would have expected "a => 
cos(a)" to work.

-Steve



More information about the Digitalmars-d-learn mailing list