| Author |
how do I get the name of a subroutine in c++
|
|
| Billy N. Patton 2004-07-28, 6: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
| |
| Lev Walkin 2004-07-28, 6: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
| |
| Peter van Merkerk 2004-07-28, 6: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
| |
| Wim Deprez 2004-07-28, 6: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
(-
| |
| Gianni Mariani 2004-07-28, 6: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.
| |
| Thomas Matthews 2004-07-28, 6: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
| |
| Gary Labowitz 2004-07-28, 6: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
| |
| Mike Smith 2004-07-28, 6: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
| |
| Lev Walkin 2004-07-28, 6: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
| |
| Jens.Toerring@physik.fu-berlin.de 2004-07-28, 6: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 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 ___ Jens.Toerring@physik.fu-berlin.de
\__________________________ http://www.toerring.de
| |
| Alan Balmer 2004-07-28, 6:19 pm |
| On Tue, 27 Jul 2004 08:20:24 -0700, Lev Walkin <vlm@lionet.info>
wrote:
>Mike Smith wrote:
>
>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
removebalmerconsultingthis@att.net
|
|
|
|