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

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for 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: myFunction

3. 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: myFunction

Where 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.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my