How to write safe and user friendly c/c++ #define macros

♀尐吖头ヾ 提交于 2019-12-22 08:36:27

问题


I have been thinking about how macros can be written to be safe, readable and intuitive. Proper use of them should be understood by the looks of them and when used incorrectly the compiler should tell you, and not let you introduce an obscure bug.

When writing multiple line define macros I usually find myself constructing them like this to fullfill the desired criteria:

#define macro(x)    do{                      \
                        ... some code line ; \
                        ... some code line ; \
                    }while(0)

This way you can both...

if (a)
{
    macro(a);
}

and...

if (a)
    macro(a);
else
{
    ...
}

A nice feature of this is that if you use them incorrectly you will get a compiler error. This will not compile for example:

if (a)
    macro(a)
else
    b();

However, I have seen SW developers read this kind of macro construct and getting quite perplexed by it. Can you think of alternative ways to write macros that will not deceive the user in a way where they do something other than expected, and still are quite understandable when browsing a new piece of code?

Also, can you find any problems with the do {} while(0) method? It could for example be a situation where you would expect the macro to behave a certain way and where this is not the case.

The reason why i am not completely convinced the do {} while(0) method is good practice is that the macros themselves look a bit weird and take some explaining to anyone (some at least) who has not seen the construct before.

Edit:

Here is an example from one of the links in the comments (thanks!) that i like. It's quite readable I think:

#define MYMACRO(a,b)   \
    if (xyzzy) asdf(); \
    else (void)0

Is it possible to break it?


回答1:


A common approach when you want your macro to do nothing, and toggle this behavior with the preprocessor, is to use the void(0) operation.

#if SOMETHINGS
#define macro(x)    do{                      \
                        ... some code line ; \
                        ... some code line ; \
                    }while(0)
#else
#define macro(x) ((void)(0))
#endif

This way you won't break compilation whenever nulling out macro operations



来源:https://stackoverflow.com/questions/23444371/how-to-write-safe-and-user-friendly-c-c-define-macros

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