Range Redesign: Empty Ranges

Ogi ogion.art at gmail.com
Thu Mar 7 12:39:51 UTC 2024


On Wednesday, 6 March 2024 at 19:03:39 UTC, Jonathan M Davis 
wrote:
> The issue is whether the function is virtual. In D, class 
> member functions are virtual by default, so you get a crash 
> when the program attempts to use the vtable to look the 
> function up (and therefore attempts to dereference null), 
> whereas in C++, you don't, because class member functions are 
> non-virtual by default, and so it doesn't do anything with the 
> vtable. If you mark the function as final in D (without it 
> overriding anything), then there shouldn't be a crash, and if 
> you mark the C++ function as virtual, then there will be.
>

Virtual or not, GCC assumes that `this` cannot be null:

```C++
#include <cassert>

class C {
public:
     bool empty() const {
          return this == nullptr; // warning: `nonnull` argument 
`this` compared to NULL
     }
};

int main()
{
     assert(((C*)nullptr)->empty()); // warning: `this` pointer is 
null
}
```




More information about the Digitalmars-d mailing list