Does C have __func__ functionality for names of the arguments of a function?

与世无争的帅哥 提交于 2019-12-06 11:58:33

问题


Does the "C" standard support something similar to __func__ for the function arguments' names?


回答1:


if you want a quick and dirty solution for this make pre-processor macros like this...

#define FUNCTION_HEADER(a) a { const char* __func__ = #a;
#define FUNCTION_FOOTER() }

... and use it for your function headers and footers like this (tested with VS 2008):

#include <windows.h>

#define FUNCTION_HEADER(a) a { const char* __func__ = #a;
#define FUNCTION_FOOTER() }

FUNCTION_HEADER( int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) )
    MessageBoxA(0, __func__, __func__, MB_OK);
    return 0;
FUNCTION_FOOTER()

This should work exactly how you want, but it is ugly.




回答2:


No, the C99 standard has the following:

6.10.8 Predefined macro names

The following macro names shall be defined by the implementation:

__DATE__ 
__FILE__ 
__LINE__ 
__STDC__ 
__STDC_HOSTED__ 
__STDC_MB_MIGHT_NEQ_WC__ 
__STDC_VERSION__ 
__TIME__ 

The following macro names are conditionally defined by the implementation:

__STDC_IEC_559__ 
__STDC_IEC_559_COMPLEX__ 
__STDC_ISO_10646__ 

6.4.2.2 Predefined identifiers

The identifier __func__ shall be 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.63)

gcc adds some extensions, as I imagine other compilers do.



来源:https://stackoverflow.com/questions/905999/does-c-have-func-functionality-for-names-of-the-arguments-of-a-function

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!