Site icon Learn C++

Learn To Use Predefined Identifier __func__ In C++

Learn To Use Predefined Identifier func In C++

Modern C++ has a lot of features that can be used in professional programmingC++11 standard improved the previous C99 standard feature __func__ which is a predefined identifier for functions or methods. The predefined identifier __func__ returns as a string that contains the name of the enclosing function or method. In this post, we explain how we can use predefined identifier __func__ in modern C++.

What is the predefined identifier __func__ in C++?

The predefined identifier __func__ returns as a string that contains the name of the enclosing function or method. In other words, it returns the string that contains the name of the function that is called from. This basic feature from C99 requires some modification to integrate with C++, and with C++11 it is improved to support strings in addition to char arrays. This a kind of constant string and the syntax is as below,

[crayon-663d111942dfb946107665/]

and here is a simple example to display function name:

[crayon-663d111942e04052392797/]

and when we run this function, it will print out its name as below.

[crayon-663d111942e06587607533/]

Is there a full example about the predefined identifier __func__ in C++?

Here is a full example about the predefined identifier __func__ in modern C++. In this example, we used __func__ in both function and method,

[crayon-663d111942e09048510261/]

here is the output,

[crayon-663d111942e0b621227077/]

This feature is generally used to capture function names as string literals and to assist diagnostic libraries. Sometimes we want to know the name of the function that is called, you can decide some sub operations by the function that is called.

For more information on this feature, see Adoption of C99’s __func__  and  __func__ predefined identifier Proposal document.

C++ Builder is the easiest and fastest C and C++ IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.

There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise version.

Exit mobile version