Home > Guides > Programming > C/C++

Toggle Open Guide Table of ContentsGuide Contents

Close Table of ContentsGuide Contents

Close Table of Contents

The __func__ Predeclared Identifier is Coming to C++

Last updated Apr 20, 2007.

C99 introduced the concept of a predeclared identifier which is used to capture function names as string literals. Document N1970 contains a proposal for adding the __func__ identifier to C++09. This addition requires some core language changes in C++. Find out all the details here.

Predeclared Identifiers

The C99 __func__ identifier is expanded as a string literal that contains the enclosing function’s name. The __func__ identifier is therefore meaningful only in the context of a function. It is expanded as the unadorned and unqualified name of the enclosing function.

C++98 doesn’t support the concept of a predefined identifier. The reserved __FILE__, __LINE__ and __DATE__ identifiers are implemented as macros that are expanded as literal strings during the preprocessing stage. Although it’s technically possible to implement the __func__ facility in C++ as a macro too, this isn’t the ideal approach. In order to expand __func__ properly, the preprocessor will have to know how to parse function definitions. Consequently, the preprocessing phase will become slower. Besides, the parsing of function definitions will have to be repeated anyway in the compilation phase to ensure that the correct assembly code is generated. Instead, C++09 adopts the notion of a predeclared identifier — a constant that the compiler defines and initializes implicitly, and which the program can access directly, as if it were a user-declared identifier.

__func__ specifications

The identifier __func__ shall be implicitly declared by the compiler as if the following declaration:

static const char __func__[] = "function-name";

appeared immediately following the opening brace of each function definition. Here, "function-name" is the unqualified and unadorned name of the enclosing function. Introducing a string literal for every function may seem very expensive. The proposal therefore gives implementers some leeway in this respect. The __func__ identifier may be optimized away when it isn’t used in a function. Furthermore, since the __func__ identifier is expanded as the unadorned name of its enclosing function, it’s possible for overload sets of the same function to share a single string literal.

The decision to use the unadorned name of a function may seem controversial as it doesn’t distinguish between overloaded versions of the same function. However, there are two reasons for preferring the unadorned name:

  • The unadorned name is compatible with C99.
  • Adorned names are ABI-specific. As such, they aren’t portable and look quite different from the programmer-given name of a function. To ensure portability, the proposal recommends that the unadorned name shall be used.

Using the unadorned name is also preferable in writing diagnostic libraries.

The assert() macro

After the addition of __func__ to C99, the assert() macro in C was updated to require the value of __func__, as well as __FILE__ and __LINE__. In C++, the assert() macro is implemented according to the C90 standard, which doesn’t include __func__. The C++ standard libraries will have to be updated accordingly, to ensure that assert() produces the same output in C++ as it does in C.

Additional Predeclared Identifier

Vendors are allowed to introduce additional predeclared identifiers. Indeed, some of them already do so. Microsoft’s Visual Studio 2005 for instance supports the macro _FUNCTION__ which translates to __func__. Visual Studio 2005 also defined the macros __FUNCDNAME__ and __FUNCSIG__ as a non-standard extension. They translate into a function’s adorned name and its signature, respectively.

A function’s adorned name can be useful for checking whether two compilers share the same ABI, for instance. Additionally, it can help you decipher cryptic linker errors. The intrepid among you may even use it to invoke a function with C++ linkage from a DLL!

The following code demonstrates the difference between Visual Studio’s __FUNCDNAME__ and __FUNCSIG__:

//Visual Studio 2005 code
void myfunc()
{
 cout<<__FUNCDNAME__endl;; //output: ?myfunc@@YAXXZ
}
struct S
{
 void myfunc() const 
 {
  Cout<<__FUNCSIG__endl;//void __thiscall S::myfunc(void) const
 }
};

Discussions

What is Required of a Move Constructor
Posted Jul 18, 2010 09:52 AM by alex14637
2 Replies
Bugzilla
Posted Nov 18, 2008 01:53 AM by cupu
2 Replies
i want c++ book through net
Posted Aug 23, 2007 11:13 PM by harivilu
3 Replies

Make a New Comment

You must log in in order to post a comment.

Related Resources

Danny KalevYves Smith: Suspicions that The Fed is manipulating Wall Street
By Danny KalevMay 24, 2010 No Comments

Yves Smith, the nom de plume of the creator of Naked Capitalism and one of the most savvy and respected members of the blogosphere. In professional life Yves is known as Susan Webber. Yves recently gave an interview to an Israeli financial newspaper in which she claims that a federal team unofficially called "the plunge protection team" is manipulating the stocks on Wall Street.

Danny KaleviPhone OS 4.0 SDK License Controversy
By Danny KalevApril 17, 2010 1 Comment

The much-discussed software development kit for the upcoming iPhone OS 4.0 says that native applications must be "originally written" in Objective C, C, or C++", forbidding developers from using any sort of "translation or compatibility layer." This legalese seems to rule out just about anything you can think of -- translating an application written in any other language to C++. However, this stern interpretation is probably exaggerated. Besides, when you consider the outcry and criticism with which Apple's license was greeted, you can't help but wondering where all these freedom of expression activists were when Sun -- and later Microsoft and Google -- imposed similar draconian restrictions on developers.

Danny KalevBjarne Stroustrups's Stevens Talk
By Danny KalevDecember 7, 2009 No Comments

On 2nd December Bjarne Stroustrup delivered a talk about the standardization process of C++0x at the Stevens institute. Here some of the key points from his talk.

See More Blogs

Informit Network