What"s the difference between __PRETTY_FUNCTION__, __FUNCTION__, __func__?

What's the difference between PRETTY_FUNCTION, FUNCTION, func?
If you are a beginner in C++ programming, chances are you have come across the terms __PRETTY_FUNCTION__, __FUNCTION__, and __func__ while reading code or researching online. But what do these terms actually mean? And more importantly, when should you use each of them? Let's dive in and unravel the mystery behind these function-related features!
Understanding the basics
Before discussing the differences between these function identifiers, it's essential to understand what they actually represent. In C++, these identifiers are predefined macros that provide you with information about the function name and other contextual details. They can be helpful for debugging and logging purposes, especially when dealing with complex codebases.
💡 Pro Tip: Macros are preprocessor directives that allow you to define reusable code snippets.
The contenders: PRETTY_FUNCTION vs. FUNCTION vs. func
1. PRETTY_FUNCTION
💡 Usage: Use __PRETTY_FUNCTION__ when compiling your code with a GNU Compiler Collection (GCC) or Clang.
__PRETTY_FUNCTION__ is a macro available in GCC and Clang that expands to a human-readable string containing the entire signature of the current function, including the return type, function name, and parameter types. This macro is incredibly useful when you need detailed information about the function in a readable format.
void myFunction() {
std::cout << __PRETTY_FUNCTION__ << std::endl;
}
// Output: void myFunction()2. FUNCTION
💡 Usage: Use __FUNCTION__ when compiling your code with a Microsoft Visual C++ (MSVC) compiler.
Similar to __PRETTY_FUNCTION__, __FUNCTION__ is a macro available in MSVC compilers. However, its expansion only includes the function name without any additional information. Although less informative than __PRETTY_FUNCTION__, it still serves as a helpful macro for logging and debugging purposes.
void myFunction() {
std::cout << __FUNCTION__ << std::endl;
}
// Output: myFunction3. func
💡 Usage: Use __func__ when writing C11-compliant code or when using a C++17 or later compliant compiler.
Introduced in the C11 standard and later adopted by C++, __func__ is a predefined identifier that expands to the name of the current function. It provides similar functionality to __FUNCTION__ but is more portable across different compilers and versions.
void myFunction() {
std::cout << __func__ << std::endl;
}
// Output: myFunctionWhere are they documented?
Now that you know the differences between these function-related macros, you might be wondering where you can find more information about them.
For __PRETTY_FUNCTION__, the official documentation can be found in the GNU Compiler Collection (GCC) manual, which details all the available predefined macros: GCC Manual - Function Names as Strings
For __func__, its documentation can be found in the C11 standard, or for C++ specific usage, refer to the C++ language standard documentation, e.g., ISO/IEC 14882:2017.
For __FUNCTION__, the documentation is available in the Microsoft Developer Network (MSDN) library, specifically in the Predefined Macros section.
Deciding which one to use
Now comes the important question: when should you use each of these function-related macros?
If you want a detailed function signature, including the return type, function name, and parameter types, and you are compiling with GCC or Clang, opt for
__PRETTY_FUNCTION__.If you only need the function name and are compiling with MSVC, then
__FUNCTION__is the macro of choice.If you are writing code that adheres to the C11 standard or using a C++17 or later compiler and need the function name without additional information,
__func__is the most portable solution.
Ultimately, the choice of macro depends on the compiler you are using and the level of detail you require. It's worth noting that these macros are not standardized, so using them might result in non-portable code. Therefore, use them judiciously and consider alternative options like std::function, typeid, or manually passing function names if cross-platform compatibility is a concern.
Wrap up
Now you understand the differences between __PRETTY_FUNCTION__, __FUNCTION__, and __func__. Whether you need a detailed function signature or just the function name, these macros can aid you in logging and debugging your code. Just make sure to choose the appropriate macro for your compiler and version.
So, the next time you're debugging a tricky function or logging valuable information, remember to leverage these macros to make your life easier! 🚀
If you found this blog post helpful, don't forget to share it with your fellow developers and leave a comment below with your favorite debugging tip! Let's debug like pros together! 👩💻👨💻
Take Your Tech Career to the Next Level
Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.


