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. 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. I’m assuming Visual Studio C++ will do the same, but, I don’t have access to a Windows machine anymore, so I can’t test the theory. If the “this” pointer gets overwritten with a null value (or gets changed in any way), you’ve got more serious problems with even getting the starting address of the method, never mind executing the line of code.
Jonathan > On Dec 2, 2015, at 2:22 PM, Jens Alfke <j...@mooseyard.com> wrote: > > In Xcode 7 some C++ code of mine has started being flagged with a new warning: > > Value.cc <http://value.cc/>:45:13: error: 'this' pointer cannot be null in > well-defined C++ code; comparison may be assumed to always evaluate to false > [-Werror,-Wtautological-undefined-compare] > if (this == NULL) return kNull; > ^~~~ ~~~~ > > Did one of the newer C++ standards declare that a non-virtual method call to > a NULL receiver is undefined behavior? Seems like an odd decision, since a > non-virtual call is just a normal function call with an implicit ‘this’ > parameter. (Obviously dereferencing any member variables of ‘this’ will > produce undefined behavior, i.e. a crash, which is why you’d do the above > test first if this is a supported thing in your class.) > > I know I could explicitly disable that warning, but (a) it makes it harder > for other people to use my code, and (b) I don’t want to go violating > standards if I can help it. > > —Jens > _______________________________________________ > Do not post admin requests to the list. They will be ignored. > Xcode-users mailing list (Xcode-users@lists.apple.com) > Help/Unsubscribe/Update your Subscription: > https://lists.apple.com/mailman/options/xcode-users/jprescott12%40icloud.com > > This email sent to jprescot...@icloud.com
_______________________________________________ Do not post admin requests to the list. They will be ignored. Xcode-users mailing list (Xcode-users@lists.apple.com) Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/xcode-users/archive%40mail-archive.com This email sent to arch...@mail-archive.com