- Overview
- Table of Contents
- Special Member Functions: Constructors, Destructors, and the Assignment Operator
- Operator Overloading
- Memory Management
- Templates
- Namespaces
- Time and Date Library
- Streams
- Object-Oriented Programming and Design Principles
- The Standard Template Library (STL) and Generic Programming
- Exception Handling
- Runtime Type Information (RTTI)
- Signal Processing
- Creating Persistent Objects
- Bit Fields
- New Cast Operators
- Environment Variables
- Variadic Functions
- Pointers to Functions
- Function Objects
- Pointers to Members
- Lock Files
- Design Patterns
- Dynamic Linking
- Tips and Techniques
- A Tour of C99
- C++0X: The New Face of Standard C++
- Reference Wrapper
- The Performance Technical Report
- auto for the People
- Ironing Templates' Syntactic Wrinkles
- Visual C++ Becomes ISO Compliant
- A Garbage Collector for C++
- C99 Core Features in C++0X
- The
shared_ptrClass - The shared_ptr Class, II
- Lambda Expressions and Closures, Part I
- Lambda Expressions and Closures, Part II
- Lambda Expressions and Closures, Part III
- The Type Traits Library, Part I
- The Type Traits Library, Part II
- The Type Traits Library, Part III
- finally Revisited
- The Any Library
- The nullptr Keyword Proposal
- Delegating Constructors
- The Explicit Conversion Operators Proposal
- Conditionally-Supported Behavior
- The weak ptr Class Template, Part I
- The weak ptr Class Template, Part II
- POD Types Revisited
- The rvalue Reference Proposal, Part I
- The rvalue Reference Proposal, Part II
- Proposal for New String Algorithms
- Concepts, Part I
- Concepts, Part II
- constexpr: Generalized Constant Expressions
- The constexpr Proposal: Constructors
- Strongly-Typed enum Types
- C++09: The Road Ahead
- C++09: Proposals by Statuses
- Changing Undefined Behavior to Diagnosable Errors
- New Character Types
- The __func__ Predeclared Identifier is Coming to C++
- Static Assertions
- The extern template Proposal
- Variadic Templates, Part I
- Variadic Templates, Part II
- Variadic Templates, Part III -- Critique
- Using unique_ptr, Part I
- Using unique_ptr, Part II
- Unrestricted Unions, Part I
- Unrestricted Unions, Part II
- Unrestricted Unions, Part III
- Types With No Linkage as Template Arguments
- New Initialization Syntax
- Initializer Lists and Sequence Constructors
- New Standard Library Algorithms
- Class Member Initializers
- Inheriting Constructors
- Introducing Attributes
- The Removal of Concepts From C++0x
- The Future of C++0x, Part I
- The Future of C++0X, Part II
- The Debate About Attributes, Part I
- The Debate About Attributes, Part II
- The Debate About Attributes, Part III
- The Debate About Attributes, Part IV
- Forward Declarations of Enum Types
- The SCARY Iterators Proposal, Part I
- The SCARY Iterators Proposal, Part II
- Heading for Deprecation: export, Exception Specification and register
- The Rejection of the Unified Function Syntax Proposal
- Rvalue References as Object Members
- FCD Approved
- The Debate on noexcept, Part I
- The Debate on noexcept, Part II
- The Debate on noexcept, Part III
- C++0x Concurrency New
- The Reflecting Circle
- We Have Mail New
- The Soapbox
- Numeric Types and Arithmetic
- Careers
- Locales and Internationalization
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
}
};


Account Sign In
View your cart