What's the best method for displaying the value of a function pointer?

R

red floyd

Given function pointer, say void (*func)(void*), what's the best way to
display it on an iostream?

as far as I can tell, there is no default overload for operator<<, and a
function pointer doesn't have a conversion to a void*.

The only options I can see are either a C-style cast to void* (unsafe
and ugly), or some kind of template:

template<class T, class U> ostream& operator<<(ostream&, const U&)

which does some kind of copy of the pointer value into an array of
unsigned char (memcpy, or some such), and then outputs the data
byte-by-byte. This, of course, has endian issues (and formatting issues
for oddball architectures such as 16-bit intel).

Any suggestions?
 
P

Pete Becker

red said:
The only options I can see are either a C-style cast to void* (unsafe
and ugly),

There's nothing unsafe about it, although it's possible (but unlikely)
that it won't give you useful information. On POSIX-conformant platforms
it's well defined.
 
R

red floyd

Pete said:
There's nothing unsafe about it, although it's possible (but unlikely)
that it won't give you useful information. On POSIX-conformant platforms
it's well defined.

I thought the Standard said something about function pointers being cast
to void*? Hence the comment about "unsafe".

[FALLACY type=compiler-specific]
I know g++ 3.2.3 gives me a warning when I use either static_cast or
reinterpret-cast.
[/FALLACY]
 
P

Pete Becker

red said:
I thought the Standard said something about function pointers being cast
to void*? Hence the comment about "unsafe".

Standard C++ doesn't allow converting a function pointer into a void*.
That doesn't mean it's "unsafe." It only means that if your compiler
allows it (which every one I use does) you might want to check the
compiler's documentation to see what it does. But any compiler that
doesn't do the obvious thing is seriously freaky.
I know g++ 3.2.3 gives me a warning when I use either static_cast or
reinterpret-cast.

Yes, that's what the standard requires. Having issued a diagnostic, the
compiler is free to do whatever the implementor chooses. If you were
writing a compiler and wanted to implement explicit conversions from
function pointers to void pointers, what would you do? (assuming the
usual architecture, where function pointers and data pointers are the
same size). There's nothing unsafe about that, is there? <g>
 
R

red floyd

Pete said:
Standard C++ doesn't allow converting a function pointer into a void*.
That doesn't mean it's "unsafe." It only means that if your compiler
allows it (which every one I use does) you might want to check the
compiler's documentation to see what it does. But any compiler that
doesn't do the obvious thing is seriously freaky.


Yes, that's what the standard requires. Having issued a diagnostic, the
compiler is free to do whatever the implementor chooses. If you were
writing a compiler and wanted to implement explicit conversions from
function pointers to void pointers, what would you do? (assuming the
usual architecture, where function pointers and data pointers are the
same size). There's nothing unsafe about that, is there? <g>

Agreed, the only place I can think of where sizeof(void*) != sizeof(void
(*)()) is in oddball stuff like '286 medium or compact model code. But
I was trying to be standard compliant. Given that the standard (in
theory) forbids such conversions, shouldn't they have provided a
portable way to output a generic function pointer?
 
R

red floyd

Pete said:
[redacted]

BTW, Pete, I'm well aware of your reputation, and who you work for, and
realize your knowledge of the Standard far exceeds mine. Please don't
take offense at my arguments :)
 
P

Pete Becker

red said:
Agreed, the only place I can think of where sizeof(void*) != sizeof(void
(*)()) is in oddball stuff like '286 medium or compact model code. But
I was trying to be standard compliant.

Given that the standard (in
theory)

and in fact.
forbids such conversions,

Just to drive it in: the standard requires a diagnostic. Nothing more.
Once the compiler issues a diagnostic it can do whatever the implementor
wants.
shouldn't they have provided a
portable way to output a generic function pointer?

Maybe, although it's not nearly as useful as being able to show the
address of a data object.
 
P

Pete Becker

red said:
Pete said:
[redacted]


BTW, Pete, I'm well aware of your reputation, and who you work for, and
realize your knowledge of the Standard far exceeds mine. Please don't
take offense at my arguments :)

I didn't take offense. Sorry if I sounded like I did. <g>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top