zoukankan      html  css  js  c++  java
  • C++库研究笔记——函数名的宏定义

    6.47 Function Names as Stringshttp://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 first of these is __func__, which is part of the C99 standard:

    The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each function 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 you 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__ is yet another name for __func__. However, in C++, __PRETTY_FUNCTION__ contains the type signature of the function as well as its bare name. For example, this program:

         extern "C" {
         extern int printf (char *, ...);
         }
         
         class a {
          public:
           void sub (int i)
             {
               printf ("__FUNCTION__ = %s
    ", __FUNCTION__);
               printf ("__PRETTY_FUNCTION__ = %s
    ", __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 not preprocessor macros. In GCC 3.3 and earlier, in C only, __FUNCTION__ and __PRETTY_FUNCTION__ were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literals. GCC 3.4 and later treat them as variables, like __func__. In C++, __FUNCTION__ and __PRETTY_FUNCTION__ have always been variables.


    但是,根据,__FUNCTION__更保险?

     

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

     

    • On GCC you can use __FUNCTION__ and __PRETTY_FUNCTION__.
    • On MSVC you can use __FUNCSIG__ and __FUNCTION__.
    simple.c: In function `main':
    simple.c:8: warning: concatenation of string literals with
    __FUNCTION__ is deprecated
    最好使用__func__?
    注:__func__是内直变量??

    Since __func__ is not a string literal, you can't concatenate it with
    another string literal, but what you can do is

    printf("%s %s ", __func__, __FILE__);

  • 相关阅读:
    qt映射器QSignalMapper的理解
    win10环境下安装Qt4.8、PyQt及development tools
    BZOJ1040 基环森林 找环+基础树形DP
    贪心 BZOJ1034
    HDU5293 树链剖分+树形DP
    BZOJ 1028 BZOJ 1029 //贪心
    BZOJ1025
    仙人掌图判定及求直径HDU3594 BZOJ1023
    BZOJ1021
    BZOJ1022
  • 原文地址:https://www.cnblogs.com/jiangu66/p/3226000.html
Copyright © 2011-2022 走看看