how do I get the name of a subroutine in c++

Discussion in 'C++' started by Billy N. Patton, Jul 27, 2004.

  1. I have :

    int main()
    {

    x();
    }

    void x(void)
    {
    if (something_goes_wrong)
    {
    cout << "function name here : some explanation here\n";
    }
    }


    I need to get the name of the function for the return of error messages


    --
    ___ _ ____ ___ __ __
    / _ )(_) / /_ __ / _ \___ _/ /_/ /____ ___
    / _ / / / / // / / ___/ _ `/ __/ __/ _ \/ _ \
    /____/_/_/_/\_, / /_/ \_,_/\__/\__/\___/_//_/
    /___/
    Texas Instruments ASIC Circuit Design Methodlogy Group
    Dallas, Texas, 214-480-4455,
     
    Billy N. Patton, Jul 27, 2004
    #1
    1. Advertising

  2. Billy N. Patton

    Lev Walkin Guest

    Billy N. Patton wrote:
    > I have :
    >
    > int main()
    > {
    >
    > x();
    > }
    >
    > void x(void)
    > {
    > if (something_goes_wrong)
    > {
    > cout << "function name here : some explanation here\n";
    > }
    > }
    >
    >
    > I need to get the name of the function for the return of error messages


    cout << __func__ << ": some explanation" << endl;

    or

    cout << __PRETTY_FUNCTION __ << ": some explanation" << endl;


    --
    Lev Walkin
     
    Lev Walkin, Jul 27, 2004
    #2
    1. Advertising

  3. Billy N. Patton wrote:

    > I have :
    >
    > int main()
    > {
    >
    > x();
    > }
    >
    > void x(void)
    > {
    > if (something_goes_wrong)
    > {
    > cout << "function name here : some explanation here\n";
    > }
    > }
    >
    >
    > I need to get the name of the function for the return of error messages


    If you are lucky your compiler supports the __func__ and/or __FUNCTION__
    macro. Since this macro is not defined in the C++ standard you cannot
    rely on it being supported by all C++ compilers. The closest alternative
    using just standard C++ are the __FILE__ and __LINE__ macro's.


    --
    Peter van Merkerk
    peter.van.merkerk(at)dse.nl
     
    Peter van Merkerk, Jul 27, 2004
    #3
  4. Peter van Merkerk wrote:
    > Billy N. Patton wrote:
    >
    >> I have :
    >>
    >> int main()
    >> {
    >>
    >> x();
    >> }
    >>
    >> void x(void)
    >> {
    >> if (something_goes_wrong)
    >> {
    >> cout << "function name here : some explanation here\n";
    >> }
    >> }
    >>
    >>
    >> I need to get the name of the function for the return of error messages

    >
    >
    > If you are lucky your compiler supports the __func__ and/or __FUNCTION__
    > macro. Since this macro is not defined in the C++ standard you cannot
    > rely on it being supported by all C++ compilers. The closest alternative
    > using just standard C++ are the __FILE__ and __LINE__ macro's.


    knit-pick - the __func__ or __FUNCTION__ symbols are not generally
    macros. They are generated in the compile phase (not the
    pre-processor). The pre-processor has no knowledge of "functions" and
    hence they are usually implemented as a special variable.
     
    Gianni Mariani, Jul 27, 2004
    #4
  5. Billy N. Patton wrote:
    > I have :
    >
    > int main()
    > {
    >
    > x();
    > }
    >
    > void x(void)
    > {
    > if (something_goes_wrong)
    > {
    > cout << "function name here : some explanation here\n";
    > }
    > }
    >
    >
    > I need to get the name of the function for the return of error messages
    >
    >

    You could _always_ do this:

    void x(void)
    {
    static const char function_name[] = "void x(void)";

    if (something_goes_wrong)
    {
    cout << function_name << ": some explanation here.\n"
    cout.flush();
    }
    }


    Your program has to have the text somewhere in the codespace,
    so it doesn't really matter whether you get it from a macro,
    compiler constant or provide it yourself. Your program will
    be more portable (to platform & other compilers) if you
    provide the text yourself.


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Jul 27, 2004
    #5
  6. "Gianni Mariani" <> wrote in message
    news:...
    > Peter van Merkerk wrote:
    > > Billy N. Patton wrote:


    > knit-pick -


    nit-pick squared - It's nit, not knit. Otherwise your argument will come
    unraveled!
    --
    Gary
     
    Gary Labowitz, Jul 27, 2004
    #6
  7. Billy N. Patton

    Mike Smith Guest

    Lev Walkin wrote:
    >
    > cout << __func__ << ": some explanation" << endl;
    >
    > or
    >
    > cout << __PRETTY_FUNCTION __ << ": some explanation" << endl;


    These are neither standard nor portable. For instance, they are not
    supported by MSVC.

    --
    Mike Smith
     
    Mike Smith, Jul 27, 2004
    #7
  8. Billy N. Patton

    Lev Walkin Guest

    Mike Smith wrote:
    > Lev Walkin wrote:
    >
    >>
    >> cout << __func__ << ": some explanation" << endl;
    >>
    >> or
    >>
    >> cout << __PRETTY_FUNCTION __ << ": some explanation" << endl;

    >
    >
    > These are neither standard nor portable. For instance, they are not
    > supported by MSVC.


    Actually, __func__ is C99 or something. And while MSVC claims compatibility
    with C language, it must support it.

    --
    Lev Walkin
     
    Lev Walkin, Jul 27, 2004
    #8
  9. Billy N. Patton

    -berlin.de Guest

    In comp.unix.programmer Lev Walkin <> wrote:
    > Mike Smith wrote:
    >> Lev Walkin wrote:
    >>
    >>>
    >>> cout << __func__ << ": some explanation" << endl;
    >>>
    >>> or
    >>>
    >>> cout << __PRETTY_FUNCTION __ << ": some explanation" << endl;

    >>
    >>
    >> These are neither standard nor portable. For instance, they are not
    >> supported by MSVC.


    > Actually, __func__ is C99 or something. And while MSVC claims compatibility
    > with C language, it must support it.


    If I didn't remember it wrong MS isn't very keen on supporting C99
    and MSVC is only C89 compliant...
    Regards, Jens
    --
    \ Jens Thoms Toerring ___ -berlin.de
    \__________________________ http://www.toerring.de
     
    -berlin.de, Jul 27, 2004
    #9
  10. Billy N. Patton

    Alan Balmer Guest

    On Tue, 27 Jul 2004 08:20:24 -0700, Lev Walkin <>
    wrote:

    >Mike Smith wrote:
    >> Lev Walkin wrote:
    >>
    >>>
    >>> cout << __func__ << ": some explanation" << endl;
    >>>
    >>> or
    >>>
    >>> cout << __PRETTY_FUNCTION __ << ": some explanation" << endl;

    >>
    >>
    >> These are neither standard nor portable. For instance, they are not
    >> supported by MSVC.

    >
    >Actually, __func__ is C99 or something. And while MSVC claims compatibility
    >with C language, it must support it.


    MS does not claim C99 compliance. Even if it did, it wouldn't need to
    support __func__ for C++. It's a different language.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Jul 27, 2004
    #10
    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. ding feng
    Replies:
    2
    Views:
    2,908
    ding feng
    Jun 25, 2003
  2. Michael Preminger

    obtaining name of the current subroutine

    Michael Preminger, Jul 16, 2004, in forum: Perl Misc
    Replies:
    1
    Views:
    106
    Vetle Roeim
    Jul 16, 2004
  3. Madhu Ramachandran

    calling subroutine , name derived from variable

    Madhu Ramachandran, Jan 6, 2006, in forum: Perl Misc
    Replies:
    26
    Views:
    261
    Matt Garrish
    Jan 9, 2006
  4. king
    Replies:
    5
    Views:
    210
  5. nazrat

    subroutine's name

    nazrat, Feb 4, 2008, in forum: Perl Misc
    Replies:
    1
    Views:
    118
    Ben Morrow
    Feb 4, 2008
Loading...

Share This Page