你知道的最难理解的 C++ 代码是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/178265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the most hard to understand piece of C++ code you know?
提问by Dror Helper
Today at work we came across the following code (some of you might recognize it):
今天在工作中我们遇到了以下代码(你们中的一些人可能认识它):
#define GET_VAL( val, type ) \
{ \
ASSERT( ( pIP + sizeof(type) ) <= pMethodEnd ); \
val = ( *((type *&)(pIP))++ ); \
}
Basically we have a byte array and a pointer. The macro returns a reference to a variable of type and advance the pointer to the end of that variable.
基本上我们有一个字节数组和一个指针。该宏返回对类型变量的引用并将指针前进到该变量的末尾。
It reminded me of the several times that I needed to "think like a parser" in order to understand C++ code.
它让我想起了多次需要“像解析器一样思考”才能理解 C++ 代码。
Do you know of other code examples that caused you to stop and read it several times till you managed to grasp what it was suppose to do?
您是否知道其他代码示例让您停下来阅读它几次,直到您设法掌握它应该做什么?
回答by Ates Goral
The inverse square root implementation in Quake 3:
Quake 3 中的平方根倒数实现:
float InvSqrt (float x){
float xhalf = 0.5f*x;
int i = *(int*)&x;
i = 0x5f3759df - (i>>1);
x = *(float*)&i;
x = x*(1.5f - xhalf*x*x);
return x;
}
Update:How this works(thanks ryan_s)
更新:这是如何工作的(感谢 ryan_s)
回答by Martin Beckett
This was on reddit recently http://www.eelis.net/C++/analogliterals.xhtml
这是最近在 reddit http://www.eelis.net/C++/analogliterals.xhtml
assert((o-----o
| !
! !
! !
! !
o-----o ).area == ( o---------o
| !
! !
o---------o ).area );
回答by Matt Rogish
Duff's Device (http://en.wikipedia.org/wiki/Duff%27s_device) give me nightmares:
Duff 的设备 ( http://en.wikipedia.org/wiki/Duff%27s_device) 让我做噩梦:
strcpy(to, from, count)
char *to, *from;
int count;
{
int n = (count + 7) / 8;
switch (count % 8) {
case 0: do { *to = *from++;
case 7: *to = *from++;
case 6: *to = *from++;
case 5: *to = *from++;
case 4: *to = *from++;
case 3: *to = *from++;
case 2: *to = *from++;
case 1: *to = *from++;
} while (--n > 0);
}
}
回答by wcm
I know it's C and not C++ but there is always the the International Obfuscated C Code Contest. I have seen some code there that would make your head spin.
我知道它是 C 而不是 C++,但总是有国际混淆 C 代码竞赛。我在那里看到了一些会让你头晕目眩的代码。
回答by mbeckish
unsigned int reverse(register unsigned int x)
{
x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1));
x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2));
x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4));
x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8));
return((x >> 16) | (x << 16));
}
Reverses the order of the bits in an int.
反转 int 中位的顺序。
回答by Ma99uS
This is well known but still impressive way to swap two integers without creating temp variable:
这是众所周知但仍然令人印象深刻的交换两个整数而不创建临时变量的方法:
// a^=b^=a^=b; // int a and int b will be swapped
// Technically undefined behavior as variable may only
// be assined once within the same statement.
//
// But this can be written correctly like this.
// Which still looks cool and unreadable ;-)
a^=b;
b^=a;
a^=b;
回答by coppro
Most Boost stuff - the template metaprogramming is bad enough, but when you factor in the workarounds necessary to get it to work on some compilers (*coughborlandcough*), it gets pretty ridiculous. Just try to understand Boost.Bind. Just try.
大多数 Boost 的东西——模板元编程已经够糟糕的了,但是当你考虑到让它在某些编译器上工作所需的变通方法 (*coughborlandcough*) 时,它变得非常荒谬。试着去理解 Boost.Bind。你试一试。
回答by Simon
C, but present in C++, I find the comma operator really obfuscates code, take this...
C,但存在于 C++ 中,我发现逗号运算符确实混淆了代码,拿这个...
ihi = y[0]>y[1] ? (inhi=1,0) : (inhi=0,1);
Terse and quite elegant, but very easy to miss or misunderstand.
简洁且相当优雅,但很容易错过或误解。
回答by Skizz
回答by Shimi Bandiel
Binary shift confuses me all the time. An example from the java.util.concurrent.ConcurrentHashMap
package:
二进制移位一直让我感到困惑。java.util.concurrent.ConcurrentHashMap
包中的一个例子:
return ((h << 7) - h + (h >>> 9) + (h >>> 17))