Home > Backend Development > C++ > What are the Differences Between `__PRETTY_FUNCTION__`, `__FUNCTION__`, and `__func__`?

What are the Differences Between `__PRETTY_FUNCTION__`, `__FUNCTION__`, and `__func__`?

Patricia Arquette
Release: 2024-12-23 21:51:10
Original
353 people have browsed it

What are the Differences Between `__PRETTY_FUNCTION__`, `__FUNCTION__`, and `__func__`?

Navigating the Differences Between PRETTY_FUNCTION__, __FUNCTION__, and __func

In the realm of coding, it's crucial to understand the distinction between three identifiers: __PRETTY_FUNCTION__, __FUNCTION__, and __func__. Each holds its unique attributes and documentation, influencing our choice depending on the desired outcome.

Diving into func

Introduced in C99, func offers a straightforward solution for accessing the current function's name within the function's scope. It expands to a character array containing the unadorned function name. This identifier has gained traction in C as well, being added in C 11 with a slightly different definition: "an implementation-defined string."

Exploring FUNCTION

Certain C compilers support FUNCTION as a pre-standard extension, but relying solely on it is not advisable. For compilers lacking func support, FUNCTION can serve as a substitute, albeit with limitations compared to its standardized counterpart.

Enter PRETTY_FUNCTION

__PRETTY_FUNCTION__, an extension specific to gcc, operates similarly to __FUNCTION__. However, in the context of C functions, it presents an enhanced version of the function name, complete with the function signature. Visual C offers a comparable alternative in __FUNCSIG__, with subtle differences.

Documentation and Availability

For the non-standard macros, it's prudent to refer to the respective compiler documentation. Visual C extensions are well-documented in MSDN under "Predefined Macros" for the C compiler, while gcc extensions are covered in the documentation page "Function Names as Strings."

Choosing the Right Identifier

Selecting the appropriate identifier depends on the desired outcome and compiler support.

  • __func__: Use this for standardized access to the unadorned function name in C99 and later.
  • __FUNCTION__: Consider using this when func is unavailable, but be aware of its limitations.
  • __PRETTY_FUNCTION__: Opt for this if you need the "pretty" function name with the signature in C code, particularly with gcc.

By understanding the nuances of these identifiers, you can harness their power effectively to obtain the function-related information you require in your coding endeavors.

The above is the detailed content of What are the Differences Between `__PRETTY_FUNCTION__`, `__FUNCTION__`, and `__func__`?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template