Get a function name programmatically in C language

Source: Internet
Author: User
Just to get the name of the function, we embed a hard-coded string in the body of the function, which is tedious and prone to error, rather than looking at how to use the new C99 feature to get the function name when the program runs.

Object reflection libraries, debugging tools, and code analyzers often need to access the name of the function at run time, until recently, the only way to accomplish this task and portable is to manually embed a hard-coded string with the function name in the function body, needless to say, this method is very monotonous and easily leads to errors. This article will show you how to use the new C99 feature to get the function name at run time.

So how do you programmatically get the function name from the currently running function?

The answer is: use __FUNCTION__ and related macros.

Elicit problems

In general, the most annoying phase of debugging is to constantly check whether a particular function has been called. The workaround for this problem is generally to add a cout or printf ()--If you use C, as follows:

void MyFunc ()
{
cout<< "MyFunc ()" <<endl;
Other code
}
Usually in a typical project, there will be thousands of functions, to add one such output statement in each function, it is undoubtedly sad to "Shushan" Ah, therefore, there is a need for a mechanism that can be done automatically to do this.

Get the name of a function

As a C + + programmer, you may often encounter macros such as __time__, __file__, and __date__, which are converted to strings that contain compile time, the name of the transformed unit to process, and the current time, at compile.

In the latest ISO C standard, as you know, C99 joins another useful, macro-like expression, __func__, which reports unmodified (that is, not cropped), the name of the function being accessed. 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";
At Function-name, for the actual function name. To activate this attribute, some compilers need to use a specific compilation flag, please review the appropriate compiler documentation for specific information.

With it, we can eliminate most of the drudgery of displaying function names by hand, and the above examples can be rewritten as follows:

void MyFunc ()
{
cout<< "__function__" <<endl;
}
Official C99 standard the __func__ identifiers defined for this purpose are indeed worth attention, however, ISO C + + does not fully support all C99 extensions, so most compiler providers use __function__ instead, and __function__ Typically a macro defined as __func__, the name is used because it has been widely supported.

In Visual Studio 2005, this attribute is active by default, but cannot be used in conjunction with the/EP and/P compilation options. Please note that in an IDE environment, you cannot recognize __func__, but instead use __function__.

Comeau users should also use __function__, not __func__.

Users of C + + Builderx should use a slightly different name: __func__.

GCC 3.0 and higher 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__ is initialized immediately after the function brace begins, the foo () and MyFunc () functions can be used safely in the argument list without worrying about overloading.

Signature and decorated name

The __function__ feature was originally designed for the C language, however, C + + programmers often needed additional information about their functions, and in Visual Studio 2005, two other non-standard extension features were supported: __funcdname__ and __funcsig __, which is translated into the decorated name and signature of a function, respectively. The decorated name of the function is useful, for example, when you want to check whether two compilers share the same ABI, and it can help you to decipher the ambiguous link errors and even invoke another C + + linked function from one DLL. In the following example, Show_name () reports the decorated name of the function:

void MyFunc ()
{
Show_name (__funcdname__); Output:? myfunc@ @YAXXZ
}
The signature of a function consists of a function name, a parameter list, a return type, and an 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 shows the difference between a separate function and a const member function signature, with the names, return types, and parameters of the two functions identical:

void MyFunc ()
{
Show_name (__funcsig__); void __cdecl myfunc (void)
}

StrUCt S
{
void MyFunc () const
{
Show_name (__funcsig__); void __thiscall s::myfunc (void) const

}
};

The above is in C language to programmatically get the contents of the function name, more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • 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.