__FILE__
and __LINE__
are well known. There is a __func__
since C99.
#include <iostream>
struct Foo {
void Do(){ std::cout << __func__ << std::endl; }
};
int main()
{
std::cout << __func__ << std::endl;
开发者_开发技巧 Foo foo; foo.Do();
return 0;
}
will output
main
Do
Is there any macro / keyword that would output method name like Foo::Do
?
Boost has a special utility macro called BOOST_CURRENT_FUNCTION that hides the differences between the compiler implementations.
Following it's implementation we see that there are several macros depending on compiler:
__PRETTY_FUNCTION__
-- GCC, MetroWerks, Digital Mars, ICC, MinGW__FUNCSIG__
-- MSVC__FUNCTION__
-- Intel and IBM__FUNC__
-- Borland__func__
-- ANSI C99
- On GCC you can use
__FUNCTION__
and__PRETTY_FUNCTION__
. - On MSVC you can use
__FUNCSIG__
and__FUNCTION__
.
There's no such macro in standard C++, and that includes the draft C++0x standard I looked at. It would complicate compilation, since parsing (necessary to determine what a function is) comes after preprocessing, and I suspect that's why there's nothing in the standard.
The __func__
you're using is nonstandard, although it apparently works on your compiler.
Not in Standard C++ (and __func__
is not part of C++). Your implementation may have such a feature though - which compiler are you using?
See "Predefined Macros (C/C++)" for a complete list supported by MS Visual Studio.
This might be useful:
http://gcc.gnu.org/onlinedocs/gcc/Function-Names.html
#include"stdio.h"
#include"stdlib.h"
int main()
{
printf("line number is %d .. func name is %s, file name is %s",__LINE__,__PRETTY_FUNCTION__,__FILE__);
return 0;
}
This is how to print line number, function name and file name in gcc.
精彩评论