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

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


当前回答

c++最恼人的解析:

struct S
{
    S() {} //default constructor
};

int main() {

    S s(); // this is not a default construction, it declares a function named s that takes no arguments and returns S.
}

其他回答

在Perl中,可以通过修改一个类的@ISA数组来改变它的继承链,这让我很惊讶。

package Employee;
our @ISA = qw(Person);
# somwhere far far away in a package long ago
@Employee::ISA = qw(Shape); 
# Now all Employee objects no longer inherit from 'Person' but from 'Shape'

Haskell's use of Maybe and Just. Maybe a is a type constructor that returns a type of Just a, but Maybe Int won't accept just an Int, it requires it to be a Just Int or Nothing. So in essence in haskell parlance Just Int is about as much of an Int as an apple is an orange. The only connection is that Just 5 returns a type of Maybe Interger, which can be constructed with the function Just and an Integer argument. This makes sense but is about as hard to explain as it can theoretically be, which is the purpose of haskell right? So is Just really JustKindaLikeButNotAtAll yea sorta, and is Maybe really a KindaLooksLikeOrIsNothing, yea sorta again.

-- Create a function that returns a Maybe Int, and return a 5, which know is definitly Int'able
>  let x :: Maybe Int; x = 5;
<interactive>:1:24:
    No instance for (Num (Maybe Int))
      arising from the literal `5' at <interactive>:1:24
    Possible fix: add an instance declaration for (Num (Maybe Int))
    In the expression: 5
    In the definition of `x': x = 5

>  Just 5  
Just 5
it :: Maybe Integer

    -- Create a function x which takes an Int
>  let x :: Int -> Int; x _ = 0;
x :: Int -> Int
-- Try to give it a Just Int
>  x $ Just 5                   

<interactive>:1:4:
    Couldn't match expected type `Int' against inferred type `Maybe t'
    In the second argument of `($)', namely `Just 5'
    In the expression: x $ Just 5
    In the definition of `it': it = x $ Just 5

祝你好运读到这篇文章,我希望它是正确的。

我认为这实际上不是一个“语言特性”(C),我很可能在发布它时很无知,但我不知道为什么会发生这种情况,所以我会问。如果它被证明与一些奇怪的语言特征有关…这真的让我很不爽,所以这个地方是值得的。

int a = 0;
int *p = &a;

printf("%d, %d, %d.\n", *p, (*p)++, *p); // Outputs "1, 0, 0.\n" on MinGW's GCC 4.4.1

Why?

——编辑

刚拿到的,没什么大不了的。我能感觉到c++大师们现在在嘲笑我。我猜函数参数计算的顺序是未指定的,所以编译器可以自由地调用它们(我想我已经在boost的文档中读到过)。在本例中,实参语句是向后求值的,这可能反映了函数的调用约定。

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"
}

我曾经写过一种编程语言,它有一个“strfry”操作符:

"hello world"?
# => "wdo rlholle"

有用的,是吗?