_ Function _ obtain the function name programmatically in C Language (not supported by vc6.0)

Source: Internet
Author: User
Just to get the function name, embed a hard-coded string in the function body. This method is tedious and easily leads to errors. Let's take a look at how to use the new c99 feature, obtain the function name when running the program.
The object reflection library, debugging tool, and Code Analyzer often need to access the function name at runtime. Not long ago, the only method that can complete this task and be portable, it is a hard-coded string that is manually embedded in the function body with the function name. Needless to say, this method is very monotonous and can easily cause errors. This article will demonstrate how to use the new c99 feature to obtain the function name at runtime.
So how can we obtain the function name from the currently running function programmatically?
The answer is: Use _ function _ and related macros.

Issue
Generally, the most disturbing stage in debugging is to constantly check whether a specific function has been called. The solution to this problem is generally to add a cout or printf () -- if you use C language, as shown below: void myfunc () {cout <"myfunc () "<Endl; // other code}
Generally, in a typical project, there are thousands of functions. To add such an output statement to each function, it is no doubt difficult to go to Shushan. Therefore, A mechanism is required to automatically complete this operation.
  
Get function name
As a C ++ programmer, macros such as _ time _, _ file _, and _ date _ are often encountered. during compilation, convert to the string containing the compilation time, the name of the conversion unit to be processed, and the current time respectively. In the latest iso c standard, c99, as you know, adds another useful macro-like expression _ FUNC __, it will report the unmodified (not cropped) and accessed function names. Note that __func _ is not a macro, because the Preprocessor knows nothing about this function; instead, it is implemented as an implicitly declared constant character array: static const char _ FUNC _ [] = "function-name"; where function-name is the actual function name. To activate this feature, Some compilers need to use a specific compilation flag. Please refer to the relevant compiler documentation for specific information.
With it, we can avoid the hassle of manually modifying the function name. The preceding example can be rewritten as follows: void myfunc () {cout <"_ function _" <Endl;} the _ FUNC _ identifier defined by the official c99 standard for this purpose deserves your attention. However, iso c ++ does not fully support all c99 extensions. Therefore, most compiler providers use _ function _ instead, _ function _ is usually a macro defined as _ FUNC _. This name is used because it has been widely supported by most people.
In Visual Studio 2005, this feature is activated by default, but cannot be used together with/EP and/P compilation options. Note that in the IDE environment, _ FUNC _ cannot be identified, instead of _ function.
Comeau users should also use _ function _ instead of _ FUNC __.
C ++ builderx users should use a slightly different name: __func __.
GCC 3.0 and later versions support both _ FUNC _ and _ function __.
Once the current function name can be automatically obtained, you can define a function that displays any function name as follows: void show_name (const char * Name) {cout <name <Endl ;} void myfunc () {show_name (_ function _); // output: myfunc} void Foo () {show_name (_ function _); // output: foo} Because _ function _ will be initialized immediately after the braces of the function start, Foo () and myfunc () functions can be safely used in the parameter list, do not worry about heavy load.

Signature and modifier name
The _ function _ feature was originally designed for the C language. However, C ++ programmers often need additional information about their functions. In Visual Studio 2005, two other non-standard extended features are also supported: __funcdname _ and _ funcsig _, which are translated into the modifier name and signature of a function respectively. Function modifiers are useful. For example, when you want to check whether two compilers share the same abi, it can also help you crack Link errors with vague meanings, and even use it to call another function linked with C ++ from one DLL. In the following example, show_name () reports the function modifier void myfunc () {show_name (_ funcdname _); // output :? Myfunc @ yaxxz}
A function signature consists of the function name, parameter list, return type, and included namespace. If it is a member function, its class name and const/volatile qualifier will also be part of the signature. The following code demonstrates the differences between an independent function and a const member function signature. The names, return types, and parameters of the two functions are identical:
Void myfunc () {show_name (_ funcsig _); // void _ cdecl myfunc (void )}
Struct s {
Void myfunc () const {show_name (_ funcsig _); // void _ thiscall S: myfunc (void) const}
};

Non-self-created articles are not original.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.