How to get function names from within functions in C++

When writing tests, it is sometimes useful to output the fully qualified name of a function that failed the test, or causes an error when debugging the program.

#include <iostream>
 
struct A
{
     void operator()()
     {
         std::cout << __PRETTY_FUNCTION__ << std::endl;
     }
};
 
int main()
{
     A a;
     a();
};

The __PRETTY_FUNCTION__ is a gcc extension that makes it possible to get the following output:

?>  ./prettyfunction
void A::operator()()

See also