Have macro 'return' a value

后端 未结 9 1842
闹比i
闹比i 2021-01-31 10:12

I\'m using a macro and I think it works fine -

#define CStrNullLastNL(str) {char* nl=strrchr(str,\'\\n\'); if(nl){*nl=0;}}

So it works to zero out the last

9条回答
  •  孤独总比滥情好
    2021-01-31 10:29

    Macro's don't return values. Macros tell the preprocessor to replace whatever is after the #define with whatever is after the thing after the #define. The result has to be valid C++.

    What you're asking for is how to make the following valid:

    func( {char* nl=strrchr(str,'\n'); if(nl){*nl=0;}} );
    

    I can't think of a good way to turn that into something valid, other than just making it a real function call. In this case, I'm not sure why a macro would be better than an inline function. That's seems to be what you're really asking for.

提交回复
热议问题