C/C + + library research notes--macro definitions for function names __jquery

Source: Internet
Author: User

Original address: C + + library research notes--macro definition of function name


6.47 Function Names as strings:http://gcc.gnu.org/onlinedocs/gcc/function-names.html

GCC provides three magic variables that hold the name of the current function, as a string. The "A" is __func__, which are part of the C99 standard:

The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each Fu Nction definition, the Declaration

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

Appeared, where Function-name is the name of the Lexically-enclosing function. This name is the unadorned name of the function.

__FUNCTION__ is another name for __func__. Older versions of GCC recognize only this name. However, it is not standardized. For maximum portability, we recommend your use __func__, but provide a fallback definition with the preprocessor:

     #if __stdc_version__ < 199901L
     # if __gnuc__ >= 2
     #  define __func__ __function__
     # Else
     #  Define __FUNC__ "<unknown>"
     # endif
     #endif

In C, __pretty_function__ are yet another name for __func__. However, in C + +, __PRETTY_FUNCTION__ contains the type signature of the FUNCTION as, as its bare name. For example:

     extern "C" {
     extern int printf (char *, ...);
     }
     
     Class A {public
      :
       void sub (int i)
         {
           printf ("__function__ =%s\n", __function__);
           printf ("__pretty_function__ =%s\n", __pretty_function__);
         }
     ;
     
     int
     main (void)
     {
       a ax;
       Ax.sub (0);
       return 0;
     }

Gives this output:

     __function__ = Sub
     __pretty_function__ = void a::sub (int)

These identifiers are are not preprocessor macros. In GCC 3.3 and earlier, in C only, __function__ and __pretty_function__ were treated as string literals; They could is used to initialize char arrays, and they could is concatenated with the other string literals. GCC 3.4 and later treat them as variables, like __func__. In C + +, __function__ and __pretty_function__ have always been variables.


But, according to, __function__ more insurance.

Macro/keyword which can be used to print out method name?

On GCC for you can use __function__ and __pretty_function__. On MSVC can use __funcsig__ and __function__. simple.c:in function ' main ':
Simple.c:8: Warning:concatenation of string literals with
__function__ is  Deprecated
Print the function name using __FUNCTION__ macro summarizes the data: it is best to use __func__. Note: __func__ is an internal straight variable.
Since __func__ is isn't a string literal, you can ' t concatenate it with
another string literal, but what your can do is

printf ("%s%s\n", __func__, __file__);

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.