C++ __func__ vs __PRETTY_FUNCTION__

__func__ and __PRETTY_FUNCTION__ are both predefined symbols in clang. __func__ is actually part of the c++ 11 specification, while __PRETTY_FUNCTION__ is supported in clang.

To see how they work you can copy and paste the following code snippet into a file, say main.cpp, and compile it with:

clang++ -std=c++11 main.cpp


#include <iostream>

using namespace std;

class myclass {
public:
  string func() const { return __func__; }
  string pretty_function() const { return __PRETTY_FUNCTION__; }
};

class otherclass {
public:
  string func() const { return __func__; }
  string pretty_function() const { return __PRETTY_FUNCTION__; }
};

int main() {
  cout << __func__ << endl;
  cout << __PRETTY_FUNCTION__ << endl;

  myclass c;
  cout << c.func() << endl;
  cout << c.pretty_function() << endl;

  otherclass oc;
  cout << oc.func() << endl;
  cout << oc.pretty_function() << endl;

  return 0;
}

When you run it you should see:

main
int main()
func
string myclass::pretty_function() const
func
string otherclass::pretty_function() const

As you can see the major difference is that __PRETTY_FUNCTION__ prints more information. It shows the type signature and containing class of the function it is contained in as well as the name, while __func__ prints only the name.

Leave a Reply

Your email address will not be published. Required fields are marked *