神秘的线" ?? ??"!在传统code [英] Cryptic line "??!??!" in legacy code

查看:120
本文介绍了神秘的线" ?? ??"!在传统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 and k are of type int *

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 is bool ? 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 into

return 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆