这是三元条件吗?:正确的(目标)C 语法?
我认为这是不可能的,但显然在 Objective C 中这是允许的:
int a = b ?: c;
所以你看到他们在这里做什么,他们省略了三元表达式的第二部分,这样如果 b 不为零,则使用 b作为第二部分。
这很聪明,但据我所知,这违反了 K&RC,可能还违反了 ANSI C。
如果不是,我多年来一直错过一个非常聪明的语法技巧......唉!
更新: 这是海湾合作委员会。
I didn't think this was possible but apparently in Objective C it is allowed:
int a = b ?: c;
So you see what they're doing here, they're leaving out the second part of the ternary expression, such that if b is nonzero, b is used as the second part.
It's clever but as far as I know this is against K&R C, and probably ANSI C.
If not, I've been missing out of a terribly clever syntax trick for years...alas!
Update:
It is gcc.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
来自http://en.wikipedia.org/wiki/%3F%3A
From http://en.wikipedia.org/wiki/%3F%3A
此行为是为
gcc
和clang
定义的。如果您正在构建 macOS 或 iOS 代码,则没有理由不使用它。不过,如果没有仔细考虑,我不会在可移植代码中使用它。
This behaviour is defined for both
gcc
andclang
. If you're building macOS or iOS code, there's no reason not to use it.I would not use it in portable code, though, without carefully considering it.
所以不,这是不允许的。在这种情况下,gcc 发出的内容是这样的:
因此,未定义的行为就是执行您在问题中所说的操作,即使您不想依赖它。
So no, it's not allowed. What gcc emits in this case does this:
So the undefined behaviour is doing what you say in your question, even though you don't want to rely on that.
这是一个 GNU C 扩展。检查编译器设置(寻找 C 风格)。不确定它是否是 Clang 的一部分,我能得到的唯一信息是在 此页面 :
This is a GNU C extension. Check you compiler settings (look for C flavor). Not sure if it's part of Clang, the only information I could get is in this page: