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

B

Billy N. Patton

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, (e-mail address removed)
 
L

Lev Walkin

Billy said:
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;
 
P

Peter van Merkerk

Billy said:
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.
 
G

Gianni Mariani

Peter said:
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.
 
T

Thomas Matthews

Billy said:
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
 
M

Mike Smith

Lev said:
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.
 
L

Lev Walkin

Mike said:
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.
 
J

Jens.Toerring

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
 
A

Alan Balmer

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.
 

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

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top