在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
在你看来,你遇到过的最令人惊讶、最怪异、最奇怪或最“WTF”的语言特性是什么?
请每个回答只回答一个特征。
当前回答
c++模板可以用来做一些奇怪的事情,最好的例子是“多维模拟字面量”,它使用模板来计算“绘制”形状的面积。下面的代码对于3x3矩形是有效的c++代码
#include"analogliterals.hpp"
using namespace analog_literals::symbols;
unsigned int c = ( o-----o
| !
! !
! !
o-----o ).area;
再举一个3D立方体的例子:
assert( ( o-------------o
|L \
| L \
| L \
| o-------------o
| ! !
! ! !
o | !
L | !
L | !
L| !
o-------------o ).volume == ( o-------------o
| !
! !
! !
o-------------o ).area * int(I-------------I) );
其他回答
在Python中,函数参数的“编译时”(或声明时)计算可能令人困惑:
def append(v, l = []):
l.append(v)
return l
print append(1)
print append(2)
>>> [1]
>>> [1,2]
其意图可能是:
def append(v, l = None):
if l is None:
l = []
l.append(v)
return l
print append(1)
print append(2)
>>> [1]
>>> [2]
这种行为对于缓存之类的事情很有用,但它可能是危险的。
附加特性:具有可变内容的元组:
a = (1,2,[3])
a[2][:] = [4] # OK
a[2] = [2] # crashes
在C:
int main() {
int i = 0;
int array[] = {1,2};
return (i[array] + 1 == array[i]);
}
这个程序将返回1 (true)。
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.
}
好吧,既然问题是断断续续的,我就加入到“有趣”中来
Go(又名Issue9)使用大写的可见性:
如果你用大写字母命名某个东西,它就会被公众访问。 如果你使用小写,它将被包保护:
包装外可见:
func Print(v ...) {
}
包外不可见
func print( v ... ) {
}
你可以在这个原始答案中找到更多。
绝对最糟糕的WTF必须是Cobol的ALTERED GOTO。
语法非常直截了当:“ALTER label1 TO GOTO label2”,但是调试运行时面条的结果令人难以置信。