在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?

请每个回答只回答一个特征。


当前回答

Forth的控制结构有些奇怪。首先,因为它是一种反向波兰符号语言,条件在IF之前,如:

x 0 = IF

现在,要关闭条件块,使用关键字THEN:

x 0 = IF ." Equals zero!" THEN

现在真正的WTF开始了。IF所做的是编译一个有条件的前向跳转,并将跳转偏移量的地址放在堆栈上。当找到THEN时,它从堆栈中弹出该地址,计算实际偏移量,然后编译它。另一方面,ELSE编译一个条件向前跳转,从堆栈中弹出一个地址,将一个新地址压入堆栈,计算弹出地址的偏移量,然后编译该偏移量。这意味着语法是这样的:

x 0 = IF ." Equals zero!" ELSE ." Not equal to zero!" THEN

第一个和第二个语句是这样编译的:

x LITERAL 0 = (0BRANCH) LITERAL offset SLITERAL" Equals zero!" (DOTQ)
x LITERAL 0 = (0BRANCH) LITERAL offset SLITERAL" Equals zero!" (DOTQ) BRANCH LITERAL offset SLITERAL" Not equal to zero!" (DOTQ)

更奇怪的是,这种行为并不隐藏。它是该语言的ANSI规范的一部分,可以通过构造自定义流控制结构或以有趣的方式组合它们来自由利用。例如,Forth的WHILE循环:

BEGIN x 10 < WHILE x 1+ to x REPEAT

BEGIN和WHILE之间的部分是任意代码,因此实际上可以在单个控制结构中让代码在条件测试之前和之后执行。这是故意的,但下面的内容虽然是允许的,但却不允许:

BEGIN DUP 2 > WHILE DUP 5 < WHILE DUP 1+ REPEAT 123 ELSE 345 THEN 

它利用了每个控制流字的工作方式来组合两个WHILE语句,并为每个出口添加不同的后循环代码来引导。为了表明我不是在开玩笑,我只是从互联网上的一个代码中复制了这个小片段,并进行了一些小的修改来简化它。

其他回答

c++中我最喜欢的一个是“公共抽象具体内联析构函数”:

class AbstractBase {
public:
    virtual ~AbstractBase() = 0 {}; // PACID!

    virtual void someFunc() = 0;
    virtual void anotherFunc() = 0;
};

我是从Scott Meyers的《Effective c++》中偷来的。看到一个方法既是纯虚拟的(通常意味着“抽象”),又是内联实现的,这看起来有点奇怪,但这是我发现的确保对象被多态破坏的最佳和最简洁的方法。

这里有一大堆奇怪的C特性:http://www.steike.com/code/useless/evil-c/

在fortran中(当然是77,可能在95中也是如此),未声明的变量和以I到N开头的参数(“In”组)将是INTEGER,所有其他未声明的变量和参数将是REAL(源)。这与“在某些情况下可选的空白”相结合,导致了最著名的错误之一。

正如弗雷德·韦伯在1990年的《另类民间传说:计算机》一书中所说:

I worked at Nasa during the summer of 1963. The group I was working in was doing preliminary work on the Mission Control Center computer systems and programs. My office mate had the job of testing out an orbit computation program which had been used during the Mercury flights. Running some test data with known answers through it, he was getting answers that were close, but not accurate enough. So, he started looking for numerical problems in the algorithm, checking to make sure his tests data was really correct, etc. After a couple of weeks with no results, he came across a DO statement, in the form: DO 10 I=1.10 This statement was interpreted by the compiler (correctly) as: DO10I = 1.10 The programmer had clearly intended: DO 10 I = 1, 10 After changing the . to a , the program results were correct to the desired accuracy. Apparently, the program's answers had been "good enough" for the sub-orbital Mercury flights, so no one suspected a bug until they tried to get greater accuracy, in anticipation of later orbital and moon flights. As far as I know, this particular bug was never blamed for any actual failure of a space flight, but the other details here seem close enough that I'm sure this incident is the source of the DO story.

我认为这是一个很大的WTF,如果DO10I被作为DO10I,并且反过来,因为隐式声明被认为是类型REAL。这是个很棒的故事。

下面这个我觉得很奇怪:

在C/ c++中,你可以有尽可能多的分号,至少在MS c++中:

int main(void)
{
 cout<<"Hello World";;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;;;
 return 0;;;;;;;;;;;;;;;;;;;;;;;
}

Algol通过名称传递(使用C语法说明):

int a[3] = { 1, 2, 3 };
int i = 1;

void f(int j)
{
    int k;
    k = j;  // k = 2
    i = 0;
    k = j;  // k = 1 (!?!)    
}

int main()
{
    f(a[i]);
}