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

Discussion in 'C++' started by red floyd, Jul 20, 2005.

  1. red floyd

    red floyd Guest

    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?
    red floyd, Jul 20, 2005
    #1
    1. Advertising

  2. red floyd

    Pete Becker Guest

    Re: What's the best method for displaying the value of a functionpointer?

    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.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jul 20, 2005
    #2
    1. Advertising

  3. red floyd

    red floyd Guest

    Re: What's the best method for displaying the value of a functionpointer?

    Pete Becker wrote:
    > 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.
    >


    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]
    red floyd, Jul 21, 2005
    #3
  4. red floyd

    Pete Becker Guest

    Re: What's the best method for displaying the value of a functionpointer?

    red floyd wrote:
    > Pete Becker wrote:
    >
    >> 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.
    >>

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

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jul 21, 2005
    #4
  5. red floyd

    red floyd Guest

    Re: What's the best method for displaying the value of a functionpointer?

    Pete Becker wrote:
    > red floyd wrote:
    >
    >> Pete Becker wrote:
    >>
    >>> 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.
    >>>

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


    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, Jul 21, 2005
    #5
  6. red floyd

    red floyd Guest

    Re: What's the best method for displaying the value of a functionpointer?

    Pete Becker wrote:
    > [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 :)
    red floyd, Jul 21, 2005
    #6
  7. red floyd

    Pete Becker Guest

    Re: What's the best method for displaying the value of a functionpointer?

    red floyd wrote:
    >
    > 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.


    I know. Mostly I was objecting to the word "unsafe." <g> Too many
    programmers have learned, erroneously, that undefined behavior causes acne.

    > 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.

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jul 21, 2005
    #7
  8. red floyd

    Pete Becker Guest

    Re: What's the best method for displaying the value of a functionpointer?

    red floyd wrote:

    > Pete Becker wrote:
    >
    >> [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>

    --

    Pete Becker
    Dinkumware, Ltd. (http://www.dinkumware.com)
    Pete Becker, Jul 21, 2005
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    702
    Ron Natalie
    Oct 10, 2003
  2. Fraser Ross
    Replies:
    4
    Views:
    1,042
    Fraser Ross
    Aug 14, 2004
  3. murgan
    Replies:
    6
    Views:
    4,861
    Thad Smith
    Dec 21, 2005
  4. Vijai Kalyan
    Replies:
    4
    Views:
    704
    Vijai Kalyan
    Nov 8, 2005
  5. Replies:
    3
    Views:
    306
    Philip Potter
    Apr 11, 2008
Loading...

Share This Page