Using comma to prevent the need for brace pair

2019-07-19 05:04发布

Sometimes, when I have a multi-case if, or a very simple for, with only two statements, I will forgo braces, instead using the comma. Is this a bad exploitation of the feature, and is it ugly and bad form? Or is it an acceptable way to save time and space?

For example:

if (something)
    b = y, c = z--;

instead of:

if (something) {
    b = y;
    c = z--;
}

7条回答
Luminary・发光体
2楼-- · 2019-07-19 06:07

The comma form is more useful for when you cannot use braces:

#define MY_ASSERT(expr) ((expr) || (debugbreak(), 0))

Here debugbreak() returns void, but we still wish to have 0 as an rvalue.

查看更多
登录 后发表回答