神秘的线" ?? ??"!在传统code [英] Cryptic line "??!??!" in legacy code
问题描述
我重构了一些非常旧的遗留code这是完全错误和非常有问题的做法,至少在现代标准。
:现在我穿过一条线,我根本无法破译跑 P
和 K
的类型为为int *
回P 10 ?? K P&* P:的sizeof(* K):!?0;
当我看到它,我简直不敢相信自己的眼睛 - 我知道
运营商,但它的语法布尔? trueresult:falseresult
和 ??
运营商既不能意义(懒惰的评价也真的不适用于此),不是我能找到一个参考神秘运营商的任何地方。
这将是非常酷的,如果有人流下了对此事的一些情况。
这就是所谓的三字符:
C11(ISO / IEC 9899:201X)§5.2.1.1的三字符序列的
在任何其他处理发生时,以下情况之一的每次出现
三个字符(称为三字符sequences17))序列被替换为
对应的单个字符。
块引用>?? =#
??([
?? / \\
??)
?? ^
??< {
??! |
??> }
?? - 〜这也是在C ++ 11(ISO / IEC 14882:2011)§2.3的三字符序列的
所以三字母置换,行
回P上之后?? ?? K P&* P:的sizeof(* K):!?0;
变成回报p || K + P + * P:的sizeof(* K):0
由于三元运营商具有相当低的precedence,它实际上是:
收益(P || K)? (第(* p)的:的sizeof(* K)):0;
I am refactoring some very old legacy code which is full of bugs and very questionable practices, at least for modern standards. Now I ran across one line which I simply cannot decipher:
p
andk
are of typeint *
return p??!??!k?p?*p:sizeof(*k):0;
When I saw it I could not believe my eyes - I know the
?
operator, but its syntax isbool ? trueresult : falseresult
and a??
operator does neither make sense (lazy evaluation does really not apply here), not could I find a reference of that mysterious operator anywhere.It would be really cool if someone shed some light on this matter.
解决方案It's called Trigraph:
C11(ISO/IEC 9899:201x) §5.2.1.1 Trigraph sequences
Before any other processing takes place, each occurrence of one of the following sequences of three characters (called trigraph sequences17)) is replaced with the corresponding single character.
??= # ??( [ ??/ \ ??) ] ??' ^ ??< { ??! | ??> } ??- ~
It's also in C++11(ISO/IEC 14882:2011) § 2.3 Trigraph sequences
So after trigraph replacement, the line
return p??!??!k?p?*p:sizeof(*k):0;
turns intoreturn p || k ? p ? *p : sizeof(*k) : 0
Since the ternary operator has a rather low precedence, it's actually:
return (p || k) ? (p ? (*p) : sizeof(*k)) : 0;
这篇关于神秘的线&QUOT; ?? ??&QUOT;!在传统code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!