开发者

Can I force the C pre processor to generate a new line? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

How can I make the preprocessor insert linebreaks into the macro expansion result?

#define IDENTIFIER { /*new line here*/\
            my_multiline(); /*new line here*/\
            macro(); /*new开发者_Go百科 line here*/\
        } /*new line here*/

Can I force the C pre-processor to generate a new line in it's expansion? I don't believe there's a standard way to do this but I wouldn't mind platform specific hacks for Visual C++ (2008) and gcc.

I'm not interested in doing this through m4 or a separate pre-processor.

Why I'm asking this:

It's more or less just curiosity. Since it's a hack I'm not going to try this in production but I'd like to know if I'm able to do this anyway. A few days ago I stumbled into this question:

Can you turn off (specific) compiler warnings for any header included from a specific location?

This is a question that I'd like a solution myself. I thought about creating a macro for including a header, but with correct pragmas to disable a warning before the including, include the header I'd like to turn warnings off, enabling the warning after the include.

Of course, I could create a script to generate mock includes with the pragma for the warning and the include.

My first problem was that since #include is a pre processor directive a macro would be somewhat useless to generate it. But then I found this answer:

Is there a way to do a #define inside of another #define?

With this I believe I can generate the special include macro if I'm able to force the compiler to generate newlines in it's expansion.

I'm at home right now but I'll post my code tomorrow when I'm at work.


If you're simply aiming to make the code more readable in a debugger, you could use inline functions instead of #defined macros. Unfortunately, the inline keyword is a C99 addition to C and only supported by proprietary extensions under ANSI C (like MSVC).

#if __STDC_VERSION__ < 199901L
# if defined(_MSC_VER)
#  define inline __inline
# elseif defined(__GNUC__)
#  define inline __inline__
# endif
#endif

static inline void IDENTIFIER() {
    my_multiline();
    macro();
}

This has the additional benefit of being type-checked (and generally less prone to error).

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜