开发者

C/C++ cool macro definitions?

开发者 https://www.devze.com 2022-12-19 21:23 出处:网络
Besides __开发者_开发问答LINE__ and __FILE__, are there other useful pre-defined macros, like __FUNCTION_NAME__?

Besides __开发者_开发问答LINE__ and __FILE__, are there other useful pre-defined macros, like __FUNCTION_NAME__?

If not, but you know of other cool/useful defined macros (especially for debugging purposes), I'd love to hear about them.

Some have asked about platform: I'm using gcc/g++ on MacOSX.


I can find the following (descriptions from C99 draft, but they are available in C89 too I think):

  • __DATE__: The date of translation of the preprocessing translation unit: a character string literal of the form "Mmm dd yyyy", where the names of the months are the same as those generated by the asctime function, and the first character of dd is a space character if the value is less than 10. If the date of translation is not available, an implementation-defined valid date shall be supplied.
  • __TIME__: The time of translation of the preprocessing translation unit: a character string literal of the form "hh:mm:ss" as in the time generated by the asctime function. If the time of translation is not available, an implementation-defined valid time shall be supplied.

For the current function name, C99 defines __func__, but __FUNCTION_NAME__ is not a standard macro. In addition, __func__ is not a macro, it's a reserved identifier (6.4.2.2p1):

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.

If you're looking for something that's platform-specific: here's a list of gcc's common predefined macros. I like __COUNTER__, which is a unique, sequential integer starting at 0. I think __INCLUDE_LEVEL__ is cool too, but not sure if I can think of a use for it yet :-).


Common GCC macros.


The specific one you're looking for is called __func__, but it's not exactly a macro, since it's meaning changes depending on where it's seen. It is useful, however, and does look like a macro.

My favorite macro at the moment is __STDC_VERSION__ because it lets me do this:

#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
# define inline
# define register
# if __GNUC__ >= 2 || _MSC_VER >= 1300
#  define __func__ __FUNCTION__
# else
#  define __func__ "<unknown>"
# endif
#endif

Now you can use the C99 keywords inline, register, and __func__ to declare things without having to worry about whether or not the compiler you're using supports that C99 functionality! In reality, the bit for inline is more complex since some compilers define __inline and other such silliness, but you get the general idea.

Also, a useful list of pre-defined macros for identifying compilers, operating systems, and architectures can be found here.


This MSDN page lists the complete set of predefined macros for MSVC.

Personally, I think __COUNTER__ is pretty cool.


If I'm not mistaken, __func__ is not a macro. It's basically a name that's declared as static const char * const __func__ = "function_name"; at the top of any function in which it is used. And you should be using __func__ instead of anything like __FUNCTION_NAME__ because __func__ is part of the C99 standard.


I tend to make single line conditions like.

#define ZZ_Slash_for_commenting_out /
#ifdef _DEBUG
#define D_B_O std::cerr
#else
#define D_B_O ZZ_Slash_for_commenting_out/
#endif
    D_B_O << "Your thing here" << endl;


You can get all the regular pre-defined macros from gcc like this:

$ gcc -dM -E - < /dev/null

Of course this doesn't include special macros like:

__FILE__, __LINE__ and __FUNCTION__.
0

精彩评论

暂无评论...
验证码 换一张
取 消