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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.