Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how do I get the name of a subroutine in c++

Reply
Thread Tools

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

 
 
Billy N. Patton
Guest
Posts: n/a
 
      07-27-2004
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, http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Lev Walkin
Guest
Posts: n/a
 
      07-27-2004
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
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Peter van Merkerk
Guest
Posts: n/a
 
      07-27-2004
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
 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-27-2004
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.
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      07-27-2004
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

 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      07-27-2004
"Gianni Mariani" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
Mike Smith
Guest
Posts: n/a
 
      07-27-2004
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
 
Reply With Quote
 
Lev Walkin
Guest
Posts: n/a
 
      07-27-2004
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
(E-Mail Removed)
 
Reply With Quote
 
Jens.Toerring@physik.fu-berlin.de
Guest
Posts: n/a
 
      07-27-2004
In comp.unix.programmer Lev Walkin <(E-Mail Removed)> 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 ___ http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de
\__________________________ http://www.toerring.de
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      07-27-2004
On Tue, 27 Jul 2004 08:20:24 -0700, Lev Walkin <(E-Mail Removed)>
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
(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
use one subroutine's variable value in another subroutine inside a module. king Perl Misc 5 04-29-2007 06:39 AM
calling subroutine , name derived from variable Madhu Ramachandran Perl Misc 26 01-09-2006 03:03 PM
obtaining name of the current subroutine Michael Preminger Perl Misc 1 07-16-2004 08:51 AM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments