In the “classic” C ++ (C ++ 98) situation is unambiguous - dereferencing the null pointer leads to undefined behavior . Accordingly, invoking a non-static object method through a null pointer results in undefined behavior . It does not matter whether this method performs access to class members or does not. This is the position of the language specification. From this point of view, you are absolutely right, and the arguments of your opponents on the topic “everything will work everywhere” are no more than a consequence of “street education” from the category of “I look into an assembler book , I see a fig.”
At the same time, attempts to form a more flexible / thin specification in this matter have been made for quite some time. In particular
DR # 232: Is indirection through a null pointer undefined behavior?
However, work in this direction has permanently hung in a state of drafting since 2005. To be honest, one gets the impression that no one can give any intelligible interpretation of the text of the current standard on this topic, perhaps precisely because the topic is still “suspended”.
As you understand, the standard will not get a separate specification for your particular case. And as soon as we move to a more general case, then immediately there are situations such as the conversion of the this pointer when the method is called under [multiple] inheritance conditions.
struct A { int a; }; struct B { int b; void foo() { // К данным мы не обращаемся // Но чему здесь равно `this`??? if (this == nullptr) ; // ??? } }; struct C : A, B { }; int main() { C *c = nullptr; c->foo(); }
It is not clear whether the compiler, when converting this pointer, in the process of calling the base class method, follows the rule "null is converted to null"? It was precisely because of such subtleties that it was originally decided to ban calls to non-static methods through a null pointer. What will be (and is) now and what are the intentions of the authors of the language - we must wait and understand.
Note, by the way, that 8.5.1.2/4 requires that the hidden parameter this when calling a class method is initialized using explicit type conversion . That is, in the above example with multiple inheritance in the c->foo() call, the B type this pointer must be initialized as (B *) c . Such a conversion works by the rule of null-in-null and the result (B *) c will also be a null pointer. However, in GCC and Clang this inside foo during the call will be 0x4 . That is, these compilers did not fulfill the requirements of 8.5.1.2/4. This immediately suggests that GCC and Clang still interpret this challenge as unspecified behavior.
PS In this case, in C, dereferencing a null pointer is strictly prohibited.