Re: New C++ warning about comparing this==NULL
Re: New C++ warning about comparing this==NULL
- Subject: Re: New C++ warning about comparing this==NULL
- From: Zack Morris <email@hidden>
- Date: Thu, 03 Dec 2015 09:30:27 -0700
I’m just going to jump in and say that I agree with Jens that this warning is a little bonkers, because I’ve used the "if(this)" check tons of times in my code to have special handling encapsulated in the class (not necessarily just error handling - there can be lots of cases where maybe I just have a placeholder). I can fully understand the argument that “this” should never be null, but to be blunt: C++ has become a nanny state and I’m tired of standards committees overreaching on how low level languages will be used. They simply cannot anticipate all of the permutations that will happen in the wild, especially with legacy code or multiple contributors.
I think probably what’s going on here is that the standard wants people to be thinking in terms of exceptions or handling this situation in whatever “modern” way has been decided upon, which probably requires a lot of boilerplate and education about this and that. My counterargument to that is that C++ probably can’t be saved at this point, because the more rules and regulations we pile onto the standard, the further we get from the original vision of C which was to have a readable assembly language (IMHO). If people want a language that protects them from themselves, they’ll just use Rust or even go the more appropriate route of writing everything in a functional language and writing low-level modules in C where performance is an issue (which in the vast majority of cases it’s not - so why are we using C again?).
To handle this practically, I wonder if an assert() could be used during debugging so that the warning goes away on the final build. Or more abstractly, maybe the preprocessor or templating could handle these cases, because ironically we want to know about the very thing that the compiler is trying to protect us from, so we’ll have to beat it to it!
Zack Morris
> On Dec 2, 2015, at 4:32 PM, Jens Alfke <email@hidden> wrote:
>
>
>> On Dec 2, 2015, at 12:00 PM, Jonathan Prescott <email@hidden> wrote:
>>
>> In C++, “this” is a read-only (const) pointer setup during construction of the instance. Once an instance of a class is successfully created, the “this” pointer is guaranteed to be non-null for the lifetime of the instance.
>
> No. It’s not a _part of_ the instance, it’s a _pointer to_ the instance. It only exists as an invisible parameter passed to a method.
>
> For example, the method foo::method(int x) is internally implemented as a function [with a mangled name] that takes a parameter list (foo *this, int x). And calling f->method(1) is exactly like calling that function with parameter list (f, 1). Assuming the method is non-virtual. If it’s virtual, this gets more complicated.
>
>> I tried out a couple of ways of trying to modify the “this" pointer of an existing class instance, and the clang and gcc compilers would not allow that operation to compile.
>
> Again, ‘this’ doesn’t belong to the instance. It’s simply a function parameter.
>
> Here’s a simple way to call a method with a NULL ‘this’:
>
> struct foo {
> void method() {
> printf(“this = %p\n”, this);
> }
> }
>
> foo *f = NULL;
> f->method();
>
> —Jens
> _______________________________________________
> Do not post admin requests to the list. They will be ignored.
> Xcode-users mailing list (email@hidden)
> Help/Unsubscribe/Update your Subscription:
>
> This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden