how do I get the name of a subroutine in c++
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Programming > how do I get the name of a subroutine in c++




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    how do I get the name of a subroutine in c++  
Billy N. Patton


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

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,  b-patton@ti.com





[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Lev Walkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

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
vlm@lionet.info





[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Peter van Merkerk


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

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





[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Wim Deprez


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

Peter van Merkerk wrote:

> Billy N. Patton wrote:
> 
>
>
> 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.
>
>

does the __func__ return "x" then? else you can insert this macro in your
code to be sure:

void x(void)
{
#define __func__ "x"
if(something_goes_wrong)
{
cout ...
}
}

can that help?

greetings,

--wim


--------
btw: yes, I _am_  embarrassed to be using outlook for the moment
(shivvvver), public computer... (or maybe i just like to live dangerously
(-









[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Gianni Mariani


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

Peter van Merkerk wrote:
> Billy N. Patton wrote:
> 
>
>
> 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.





[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Thomas Matthews


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

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






[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Gary Labowitz


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

"Gianni Mariani" <gi2nospam@mariani.ws> wrote in message
news:41065947.6010902@mariani.ws...
> Peter van Merkerk wrote: 
[vbcol=seagreen]
> knit-pick -

nit-pick squared - It's nit, not knit. Otherwise your argument will come
unraveled!
--
Gary







[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Mike Smith


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

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





[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Lev Walkin


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

Mike Smith wrote:
> Lev Walkin wrote:
> 
>
>
> 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
vlm@lionet.info





[ Post a follow-up to this message ]



    Re: how do I get the name of a subroutine in c++  
Jens.Toerring@physik.fu-berlin.de


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
07-28-04 11:19 PM

In comp.unix.programmer Lev Walkin <vlm@lionet.info> wrote:
> Mike Smith wrote: 
[vbcol=seagreen]
> Actually, __func__ is C99 or something. And while MSVC claims compatibilit
y
> 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  ___  Jens.Toerring@physik.fu-berlin.de
\__________________________  http://www.toerring.de





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 11:17 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register