Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

c

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

like image 863
Notinlist Avatar asked Feb 03 '10 14:02

Notinlist


People also ask

What is __ function __ in C?

(C++11) The predefined identifier __func__ is implicitly defined as a string that contains the unqualified and unadorned name of the enclosing function. __func__ is mandated by the C++ standard and is not a Microsoft extension.


2 Answers

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
like image 180
Kornel Kisielewicz Avatar answered Oct 14 '22 20:10

Kornel Kisielewicz


  • On GCC you can use __FUNCTION__ and __PRETTY_FUNCTION__.
  • On MSVC you can use __FUNCSIG__ and __FUNCTION__.
like image 27
rpg Avatar answered Oct 14 '22 21:10

rpg