How bad is “if (!this)” in a C++ member function?

前端 未结 11 690
不思量自难忘°
不思量自难忘° 2021-02-04 23:16

If I come across old code that does if (!this) return; in an app, how severe a risk is this? Is it a dangerous ticking time bomb that requires an immediate app-wide

11条回答
  •  星月不相逢
    2021-02-04 23:44

    Like all undefined behavior

    if (!this)
    {
       return NULL;
    }
    

    this is a bomb waiting to go off. If it works with your current compilers, you are kind-of lucky, kind-of unlucky!

    The next release of the same compilers might be more aggressive and see this as dead code. As this can never be null, the code can "safely" be removed.

    I think it is better if you removed it!

提交回复
热议问题