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

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


当前回答

有向图和替代标记

C (ISO/IEC 9899:1999, 6.4.6/3)和c++ (ISO/IEC 14882:2003, 2.5)有一个很少使用的特性,被C称为“有向图”,被c++称为“替代令牌”。它们不同于三trigraph,主要是因为包含它们的字符串字面量永远不会被不同地解释。

%:include <stdio.h>

int main() <%
    int a<:10:> = <%0%>;
    printf("Here's the 5th element of 'a': %d\n", a<:4:>);
    puts("Evil, eh? %:>");
    return 0;
%>

c++有更多,包括and, or, and not,它们被要求表现为&&,||和!C语言也有这些,但是需要包含<iso646.h>来使用它们,将它们视为宏而不是令牌。c++头文件<ciso646>实际上是一个空文件。

值得注意的是,GCC实现了对这种奇怪的语言特性的支持,但许多其他编译器在尝试编译上述代码段时都阻塞并死亡。

其他回答

Perl。

输出“Foo\n”除非$ Foo;

C + + 1 xλ的:

[] (int x) { std::cout << x << std::endl; } ();

它们可能被滥用在一些奇怪的语法中:

[](){}();[]{[]{}();}();

这是完全有效的c++ 1x。

在ruby/python/c中,你可以像这样连接字符串:

a = "foo" "bar"
print a # => "foobar"

c++模板机制是图灵完备的:只要在运行时不需要输入,就可以在编译时进行任意计算。任意的。或者你可以很容易地编写一个永远不会编译的c++程序——但是语法是正确的。

INTERCAL可能是最奇怪的语言特征的最佳汇编。我个人最喜欢的是COMEFROM语句,它(几乎)与GOTO相反。

COMEFROM is roughly the opposite of GOTO in that it can take the execution state from any arbitrary point in code to a COMEFROM statement. The point in code where the state transfer happens is usually given as a parameter to COMEFROM. Whether the transfer happens before or after the instruction at the specified transfer point depends on the language used. Depending on the language used, multiple COMEFROMs referencing the same departure point may be invalid, be non-deterministic, be executed in some sort of defined priority, or even induce parallel or otherwise concurrent execution as seen in Threaded Intercal. A simple example of a "COMEFROM x" statement is a label x (which does not need to be physically located anywhere near its corresponding COMEFROM) that acts as a "trap door". When code execution reaches the label, control gets passed to the statement following the COMEFROM. The effect of this is primarily to make debugging (and understanding the control flow of the program) extremely difficult, since there is no indication near the label that control will mysteriously jump to another point of the program.