How to get function name in C++

Being able to access and print the function name from any location inside a C++ function can be useful for investigating programs.

There seems to be 3 different function name strings available:

  • __func__ is a constant that only holds the name of the function. It is part of the C and C++ standards.
int A::foobar(const std::vector<float>& fvec, bool isDragon)
{
    std::cout << __func__ << std::endl;
    // Output: foobar
}
  • __FUNCTION__ is a GCC constant whose output is exactly the same as __func__.

  • __PRETTY_FUNCTION__ is a GCC constant that is more useful for C++, because it holds the entire function signature:

int A::foobar(const std::vector<float>& fvec, bool isDragon)
{
    std::cout << __func__ << std::endl;
    // Output: int A::foobar(const std::vector<float>&, bool)
}

Reference: GCC: Function names as strings

Advertisements