开发者

Macro / keyword which can be used to print out method name?

__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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜