473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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-******@ti.com
Jul 22 '05 #1
9 1472
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
vl*@lionet.info
Jul 22 '05 #2
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
Jul 22 '05 #3
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.
Jul 22 '05 #4
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

Jul 22 '05 #5
"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:41**************@mariani.ws...
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
Jul 22 '05 #6
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
Jul 22 '05 #7
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
vl*@lionet.info
Jul 22 '05 #8
In comp.unix.programmer Lev Walkin <vl*@lionet.info> 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 ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Jul 22 '05 #9
On Tue, 27 Jul 2004 08:20:24 -0700, Lev Walkin <vl*@lionet.info>
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
re************************@att.net
Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Michael Farber | last post by:
Not sure if this is the right group for this but anyways... I've got an ASP web application that uses a Visual Basic component to do some work. I instantiate the component in asp and then...
2
by: Jim Heavey | last post by:
Hello, I was wondering if there is some way I can obtain the name of the subroutine that a line of code is executing in via the code. Sy for example you wanted to write a message like the...
2
by: singlal | last post by:
Hi, my question was not getting any attention because it moved to 2nd page; so posting it again. Sorry for any inconvenience but I need to get it resolved fast. Need your help! ...
1
by: GH | last post by:
How is it possible to pass a control name (possibly one of several) to a subroutine in VB.Net (for aspx page) I have multiple gridviews on a page and one of them will be exported to Excel,...
1
by: peterv6 | last post by:
I'm using a "package" type subroutine, called test_package.pl. I'm calling it from a script called split0.pl. I want to pass the $0 variable, use the subroutine to split out just the filename, and...
4
by: otterbyte | last post by:
Hi, I have a bit of code which is confusing me to no end. Here are the basics: 1) The class module is being used in the module of a form. 2) There is an instance of the object declared at the...
3
by: sangith | last post by:
Hi, I have question on processing the file handle in a subroutine. Here is my program without subroutine: open FH1, "<outfile" or die "cannot open the file for reading: $!\n"; while...
3
by: Goran Djuranovic | last post by:
Hi all, Is there a way to retrieve a derived class name inside a subroutine or a function of the base class? I am trying to get some data from the database, based on which derived class is calling...
3
shrek123
by: shrek123 | last post by:
How can I pass output of some perl subroutine to a subroutine? I have Subroutine1 and wanna pass the return value of this subroutine as an argument to another subroutine. I tried this; ...
1
by: bassi.carlo | last post by:
I have an application with many combobox in different forms used for choose a particular length (for example items are "ft" - feet, "in" - inch, "m" - meters, and so on). I don't want to put in...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.