"Alf P. Steinbach" <
alf.p.stein...@gmail.com> writes:
> On 23.06.2016 20:53, Ben Bacarisse wrote:
>> Ghost <
n...@spam.net> writes:
>>
>>> I find myself in a situation where I can only pass around
>>> structs with no pointer members. However, I would like to
>>> include information about function pointers. Is it possible
>>> to cast a function pointer into some primitive type and
>>> back in a safe and portable way?
>>
>> I don't think you can do it with a cast in a 100% portable way. You can
>> include cstdint and test for UINTPTR_MAX which will tell you if there is
>> a type uintptr_t type that can hold a pointer value (or, alternatively,
>> INTPTR_MAX for the signed version) but even that is only guaranteed to
>> work for object pointers.
>
> Since C++11 an implementation can support casting function pointer to
> void* and back. Essentially because the Posix API requires that. And
> then uintptr_t is necessarily large enough.
Yes, I wasn't very clear at all. I should have pointed out that
converting to uintptr_t only works for void * (I think C++ defers to C
on this matter) so you can't ever convert a function pointer directly.
(in the standard sense that is -- it is, of course, very likely to work
in practice). Thus two things are needed: the uintptr_t type and the
(two-way) conversion between function pointers and object pointers.
> C++11 §5.2.10/8:
Thanks for the quote. I didn't know this happened so late!
<snip>
>> The only portable method would be to copy the representation:
<snip>
> But I'd go for the portable-enough uintptr_t. ;-)
Sure, me too, but the OP's request was so odd (why no function pointers
in the structures?) that they may be looking at some very odd hardware
that really can't do it any other way.
--
Ben.