您所知道的最难理解的C ++代码是什么? [英] What is the most hard to understand piece of C++ code you know?

查看:55
本文介绍了您所知道的最难理解的C ++代码是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天在工作中,我们遇到了以下代码(有些人可能会认识到):

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.

它使我想起了几次我需要像解析器一样思考"才能理解C ++代码.

It reminded me of the several times that I needed to "think like a parser" in order to understand C++ code.

您是否知道其他导致停下来阅读几次的代码示例,直到您完全掌握了应该做的事情?

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?

推荐答案

雷神之锤3中的平方根倒数实现

The inverse square root implementation in 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;
}

更新:工作原理(感谢ryan_s)

Update: How this works (thanks ryan_s)

这篇关于您所知道的最难理解的C ++代码是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆