Connecting Tech Pros Worldwide Forums | Help | Site Map

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

red floyd
Guest
 
Posts: n/a
#1: Jul 23 '05

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?


Pete Becker
Guest
 
Posts: n/a
#2: Jul 23 '05

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


red floyd wrote:[color=blue]
>
>
> The only options I can see are either a C-style cast to void* (unsafe
> and ugly),
>[/color]

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.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
red floyd
Guest
 
Posts: n/a
#3: Jul 23 '05

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


Pete Becker wrote:[color=blue]
> red floyd wrote:
>[color=green]
>>
>>
>> The only options I can see are either a C-style cast to void* (unsafe
>> and ugly),
>>[/color]
>
> 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.
>[/color]

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]
Pete Becker
Guest
 
Posts: n/a
#4: Jul 23 '05

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


red floyd wrote:[color=blue]
> Pete Becker wrote:
>[color=green]
>> red floyd wrote:
>>[color=darkred]
>>>
>>>
>>> The only options I can see are either a C-style cast to void* (unsafe
>>> and ugly),
>>>[/color]
>>
>> 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.
>>[/color]
>
> I thought the Standard said something about function pointers being cast
> to void*? Hence the comment about "unsafe".[/color]

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.
[color=blue]
>
> I know g++ 3.2.3 gives me a warning when I use either static_cast or
> reinterpret-cast.
>[/color]

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>

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
red floyd
Guest
 
Posts: n/a
#5: Jul 23 '05

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


Pete Becker wrote:[color=blue]
> red floyd wrote:
>[color=green]
>> Pete Becker wrote:
>>[color=darkred]
>>> red floyd wrote:
>>>
>>>>
>>>>
>>>> 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.
>>>[/color]
>>
>> I thought the Standard said something about function pointers being
>> cast to void*? Hence the comment about "unsafe".[/color]
>
>
> 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.
>[color=green]
>>
>> I know g++ 3.2.3 gives me a warning when I use either static_cast or
>> reinterpret-cast.
>>[/color]
>
> 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>
>[/color]

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?

red floyd
Guest
 
Posts: n/a
#6: Jul 23 '05

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


Pete Becker wrote:[color=blue]
> [redacted][/color]

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 :-)


Pete Becker
Guest
 
Posts: n/a
#7: Jul 23 '05

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


red floyd wrote:[color=blue]
>
> 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.[/color]

I know. Mostly I was objecting to the word "unsafe." <g> Too many
programmers have learned, erroneously, that undefined behavior causes acne.
[color=blue]
> Given that the standard (in
> theory)[/color]

and in fact.
[color=blue]
> forbids such conversions,[/color]

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.
[color=blue]
> shouldn't they have provided a
> portable way to output a generic function pointer?
>[/color]

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

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Pete Becker
Guest
 
Posts: n/a
#8: Jul 23 '05

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


red floyd wrote:
[color=blue]
> Pete Becker wrote:
>[color=green]
>> [redacted][/color]
>
>
> 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 :-)
>[/color]

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

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Closed Thread