关于声明的螺旋规律 - 当它是错误的? [英] The spiral rule about declarations — when is it in error?

查看:122
本文介绍了关于声明的螺旋规律 - 当它是错误的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

笔者近日了解到螺旋规则获得deobfuscating复杂的声明,必须已经写了一系列的类型定义。但是,下面的评论我报警:

I recently learned the spiral rule for deobfuscating complex declarations, that must have been written with a series of typedefs. However, the following comment alarms me:

<一个href=\"http://stackoverflow.com/questions/7526152/easy-rule-to-read-complicated-const-declarations/7526298#comment23229477_7526298\">A经常被引用的简化,只适用于一些简单的情况。

我没有找到无效(*信号(INT,无效(* FP)(INT)))(INT); 一个简单的例子。这是所有的更令人震惊的,顺便说一句。

I do not find void (*signal(int, void (*fp)(int)))(int); a "simple case". Which is all the more alarming, by the way.

所以,我的问题是,在哪些情况下会我是正确的应用规则,并在其中这将是错误的?

推荐答案

从根本上说,规则根本不起作用,否则
通过重新定义什么是由螺旋指工作(在这种情况下,
有一个在它没有任何意义。举个例子:

Basically speaking, the rule simply doesn't work, or else it works by redefining what is meant by spiral (in which case, there's no point in it. Consider, for example:

int* a[10][15];

螺旋规则将给出一个是指针数组[10]到
数组[15]为int,这是不对的。它的情况下,你的网站,它
不工作,要么;事实上,在信号的情况下,它不是
甚至不清楚,你应该开始螺旋。

The spiral rule would give a is an array[10] of pointer to array[15] of int, which is wrong. It the case you site, it doesn't work either; in fact, in the case of signal, it's not even clear where you should start the spiral.

在一般情况下,它更容易找到的地方规则失败的例子
比例子,它的工作原理。

In general, it's easier to find examples of where the rule fails than examples where it works.

我经常忍不住说,解析C ++声明
简单,但没有身体谁曾与复杂的声明试图
会相信我。在另一方面,这并不难,因为它是
有时候做出来的人。秘密就是想的
声明完全按照自己的一个前pression,但有很多
更少的运营商,和一个非常简单的precedence规则:所有的运营商
向右拥有对所有运营商向左侧precedence。在
如果没有括号,这意味着过程一切交给
先权,那么一切到左边,并处理
正是括号,你会在任何其他前pression。该
实际困难是<青霉>不的语法本身,但它
结果是一些非常复杂和反直觉的声明,
特别是当函数返回值和指针
功能涉及:第一个路口右转,然后离开规则;
即在一个特定水平运营商经常广泛分离,
例如:

I'm often tempted to say that parsing a C++ declaration is simple, but no body who has tried with complicated declarations would believe me. On the other hand, it's not as hard as it is sometimes made out to be. The secret is to think of the declaration exactly as you would an expression, but with a lot less operators, and a very simple precedence rule: all operators to the right have precedence over all operators to the left. In the absence of parentheses, this means process everything to the right first, then everything to the left, and process parentheses exactly as you would in any other expression. The actual difficulty is not the syntax per se, but that it results is some very complex and counterintuitive declarations, in particular where function return values and pointers to functions are involved: the first right, then left rule means that operators at a particular level are often widely separated, e.g.:

int (*f( /* lots of parameters */ ))[10];

在这里扩张的最后期限是 INT [10] ,但将
[10] 至少对我来说)非常不自然,我不得不停下工作了
每一次。 (这也可能是这种趋势在逻辑上相邻
配件为s $ P $垫出导致螺旋规则。问题
是,当然,在不存在括号的,他们不
始终使用s $ P $垫出&MDASH;任何时候你看到 [I] [J] ,规则是去
右,再右拐走,而不是螺旋。)

The final term in the expansion here is int[10], but putting the [10] after the complete function specification is (at least to me) very unnatural, and I have to stop and work it out each time. (It's probably this tendency for logically adjacent parts to spread out that lead to the spiral rule. The problem is, of course, that in the absence of parentheses, they don't always spread out—anytime you see [i][j], the rule is go right, then go right again, rather than spiral.)

既然我们现在在思维声明
前pressions:你是做什么的时候一个前pression变得太
复杂的阅读?您介绍为了中间变量
以使其更易于阅读。在声明中,的情况下,
中间变量都是的typedef 。特别是,我愿意
认为,返回类型中的任何一部分时间后结束
函数参数(和很多其他时间也一样),你
应使用的typedef 来作出声明简单。 (这个
是一个听我的话,还不如我做的规则,但是。恐怕
我会偶尔使用一些非常复杂的声明。)

And since we're now thinking of declarations in terms of expressions: what do you do when an expression becomes too complicated to read? You introduce intermediate variables in order to make it easier to read. In the case of declarations, the "intermediate variables" are typedef. In particular, I would argue that any time part of the return type ends up after the function arguments (and a lot of other times as well), you should use a typedef to make the declaration simpler. (This is a "do as I say, not as I do" rule, however. I'm afraid that I'll occasionally use some very complex declarations.)

这篇关于关于声明的螺旋规律 - 当它是错误的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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