Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.lang.c++.moderated    |    Moderated discussion of C++ superhackery    |    33,346 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 31,658 of 33,346    |
|    =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All    |
|    Re: Reference to show that if (this == N    |
|    10 Nov 11 11:08:51    |
   
   73f140ae   
   From: daniel.kruegler@googlemail.com   
      
   Am 09.11.2011 00:52, schrieb Roman Kutlak:   
   >> Wait a second --- notice that I said I'm trying to convince   
   >> someone that the trick invokes undefined behaviour;   
   >   
   > Hi. I had a look at the c++11 standard and as far as I can tell the   
   > syntax and the semantics are defined.   
   >   
   > 4.10 Pointer conversions [conv.ptr]   
   > 1 A null pointer constant is an integral constant expression (5.19)   
   > prvalue of integer type that evaluates to zero or a prvalue of type   
   > std::nullptr_t. A null pointer constant can be converted to a pointer   
   > type;   
   >   
   > I interpret this as ((Foo*)0) converts to Foo* with no problems.   
      
   I agree but this does has no relation to the problem mentioned by the OP.   
      
   > 9.3.2 The this pointer [class.this]   
   > 1 In the body of a non-static (9.3) member function, the keyword this   
   > is a prvalue expression whose value is the address of the object for   
   > which the function is called. ...   
   >   
   > This just says (to me anyway) that 'this' will be treated as a pointer   
   > to the object that called the function. And since the function is non-   
   > virtual member function, the call to left_node()->sum() will be   
   > interpreted as Node::sum(NULL).   
      
   The wording you are quoting just describes the semantics of this. This   
   wording does not say *anything* about whether the 'this' pointer might   
   be a null pointer or not. This wording is not relevant for the question   
   asked.   
      
   > The undefined behaviour comes from accessing data members because   
   > 'this' points to 0 (non-existent object) or calling virtual methods   
   > (no table to look up the available functions).   
      
   These criteria are not sufficient. Additional to lvalue-to-rvalue   
   conversion of an "empty" lvalue (such as *((Foo*)0)) the standard makes   
   it undefined, if the so-called object-expression (This is the left side   
   of any class member access E1.E2; E1->E2 has the semantics of   
   (*(E1)).E2) does not refer to a valid object if E2 is a non-static   
   member. Note that for a static member, the call expression is   
   well-formed if applied to a NULL pointer. E.g.   
      
   struct A {   
    static void f() {}   
   };   
      
   int main() {   
    ((A*) 0)->f();   
   }   
      
   is OK, see   
      
   http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_closed.html#315   
      
   for further details.   
      
   HTH & Greetings from Bremen,   
      
   Daniel Krügler   
      
      
   --   
    [ See http://www.gotw.ca/resources/clcm.htm for info about ]   
    [ comp.lang.c++.moderated. First time posters: Do this! ]   
      
   --- SoupGate-Win32 v1.05   
    * Origin: you cannot sedate... all the things you hate (1:229/2)   
|
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca