问题
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