[Issue 13247] switch doesn't work with pointers to functions. Also casting pointers to functions to integers doesn't work during compilation.

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Mon Aug 4 11:42:21 PDT 2014


https://issues.dlang.org/show_bug.cgi?id=13247

--- Comment #4 from yebblies <yebblies at gmail.com> ---
(In reply to Andrei Alexandrescu from comment #3)
> (In reply to yebblies from comment #2)
> > While you can do some things with a function pointer at compile time, you
> > _cannot_ convert it to an integer.
> 
> Can you elaborate? Couldn't the linker just fix up the address as it does
> for calls?

I think for both integer enums and final switch, the integral value needs to be
known at compile-time.  Knowing at link-time isn't enough.  How else do you
ensure cases are distinct?

This is probably _possible_ to implement, but you won't get any nice jump-table
optimizations out of the switch anyway.

Actually, with ASLR function addresses may not be known until load time.  I'm
not sure exactly how this would work out in PIC, but I doubt it's pretty.

> 
> > I'm not sure why you would expect switch to work on function pointers.
> 
> In std.variant, sometimes it's better to just switch on the tag (which is a
> pointer to function) instead of making an indirect call through it.

I think it's quite reasonable for an advanced linker or compiler to merge
functions with the same body.  This would mean function pointers are not
necessarily distinct.  I think it can break the other way too - the same
function can have different addresses, if it is compiled into two different
modules (ie dlls/shared objects).

--


More information about the Digitalmars-d-bugs mailing list