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

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


当前回答

C的多个名称空间:

typedef int i;

void foo()
{
    struct i {i i;} i;
    i: i.i = 3;
    printf( "%i\n", i.i);
}

或与字符:

typedef char c;

void foo()
{
    struct c {c c;} c;
    c: c.c = 'c';
    printf( "%c\n", c.c);
}

其他回答

c++(或Java)中没有封装的事实。任何对象都可以违反任何其他对象的封装,破坏它的私有数据,只要它是相同的类型。例如:

#include <iostream>
using namespace std;

class X
{
  public:
    // Construct by passing internal value
    X (int i) : i (i) {}

    // This breaks encapsulation
    void violate (X & other)
    {
        other.i += i;
    }

    int get () { return i; }

  private:
    int i;
};

int main (int ac, char * av[])
{
    X a(1), b(2), c(3);

    a.violate (c);
    b.violate (c);
    cout << c.get() << endl;    // "6"
}

另一个C-ism。

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

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

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

Javascript中有很多东西会让你流泪。

局部变量的作用域,举个简单的例子:

function foo(obj)
{
  for (var n = 0; n < 10; n++)
  {
    var t;        // Here is a 't'
    ...
  }
  t = "okay";     // And here's the same 't'
}

一个意想不到的特性是,在C、c#、Ruby等语言中,枚举定义列表和数组初始化列表中的末尾逗号。

string[] foods = { "tofu", "grits", "cabbage", }

public enum ArtPeriod {
  Modern,
  Romantic,
  Dada,
}

这并不是一个真正的语言特性,而是一个实现缺陷:一些早期的Fortran编译器通过使用常量池来实现常量。所有参数都是通过引用传递的。如果你调用一个函数,例如。

f(1)

编译器会将常量池中常量1的地址传递给函数。 如果您为函数中的参数赋值,则会在程序中全局地更改该值(在本例中为1)。引起了一些挠头。