📅 2018-Aug-22 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ function name, gcc, visual cpp ⬩ 📚 Archive
Being able to access and print the function name from any location inside a C++ function can be useful for investigating programs.
There are many ways to obtain the function name in C++:
__func__
is a constant that holds the name of the function:int A::foobar(const std::vector<float>& fvec, bool isDragon)
{std::cout << __func__ << std::endl;
// Output: foobar
}
This is the only constant related to the function name that is defined in the C and C++ standards. The C++11 standard §8.4 describes this constant:
The function-local predefined variable
__func__
is defined as if a definition of the formstatic const char __func__[] = "function-name ";
had been provided, wherefunction-name
is an implementation-defined string.
Though the standards provide compilers the freedom to store whatever function details they want in this string, all the compilers that I have tried store only the function name in this string.
__FUNCTION__
is a GCC and Visual CPP constant whose output is exactly the same as __func__
.
__PRETTY_FUNCTION__
is a GCC constant that is 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)
}
__FUNCDNAME__
is a Visual CPP constant holding the mangled function name.
__FUNCSIG__
is a Visual CPP constant holding the function signature with information about input, output and template types.
Example program and its output with GCC compiler:
#include <iostream>
template <typename T>
void foo(T i)
{std::cout << __func__ << std::endl;
std::cout << __FUNCTION__ << std::endl;
std::cout << __PRETTY_FUNCTION__ << std::endl << std::endl;
}
int main()
{int>(10);
foo<float>(3.14);
foo<return 0;
}
// Output:
// foo
// foo
// void foo(T) [with T = int]
//
// foo
// foo
// void foo(T) [with T = float]
#include <iostream>
template <typename T>
void foo(T i)
{std::cout << __func__ << std::endl;
std::cout << __FUNCTION__ << std::endl;
std::cout << __FUNCDNAME__ << std::endl;
std::cout << __FUNCSIG__ << std::endl << std::endl;
}
int main()
{int>(10);
foo<float>(3.14);
foo<return 0;
}
// Output:
// foo
// foo
// ??$foo@H@@YAXH@Z
// void __cdecl foo<int>(int)
//
// foo
// foo
// ??$foo@M@@YAXM@Z
// void __cdecl foo<float>(float)
References: