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

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


当前回答

这并不是说它被大量使用,而是c++的“返回对静态大小数组的引用”的语法很奇怪:

struct SuperFoo {
  int (&getFoo() const)[10] {
    static int foo[10];
    return foo;
  }
}

在上述情况下,Ofc方法可以声明为静态const

其他回答

Python的everything-is-really-a-reference有一个有趣的副作用:

>>> a = [[1]] * 7
>>> a
[[1], [1], [1], [1], [1], [1], [1]]
>>> a[0][0] = 2
>>> a
[[2], [2], [2], [2], [2], [2], [2]]

我喜欢在C中插入八进制值:

int values[8] = { 123, 154, 103, 310, 046, 806, 002, 970 };

在Ruby中…

i=true
while(i)
   i=false
   a=2
end
puts defined?(a) // returns true

在JavaScript中,下面的构造

return
{
    id : 1234,
    title : 'Tony the Pony'
};

返回undefined是一个语法错误,因为在返回后的换行上偷偷地插入了分号。以下工作正如你所期望的那样:

return {
    id : 1234,
    title : 'Tony the Pony'
};

更糟糕的是,这个也可以(至少在Chrome中):

return /*
*/{
    id : 1234,
    title : 'Tony the Pony'
};

下面是同一个问题的一个变种,它不会产生语法错误,只是无声地失败了:

return
    2 + 2;

另一个C-ism。

int i= 0;
while( i != 12 ) {
    /* Some comment 
    i += 1;
    /* Another comment */
}

为什么不行?棉绒会告诉你。然而,C编译器通常会轻松地忽略这一点。我也是。

当我发现问题出在哪里的时候,那真是太棒了。