home bbs files messages ]

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 32,383 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to All   
   Re: Q: g++-Bug or standard-conform?: tak   
   07 Jun 12 14:08:44   
   
   5d0c6c69   
   From: daniel.kruegler@googlemail.com   
      
   Am 07.06.2012 21:07, schrieb Claudius:   
   > Hello, Is this a bug in the g++-4.6.3 ?   
   >   
   > Here is a complete test program, see the comments at the bottom.   
      
   I'm seeing two times the same(?) program. I assume that this is an   
   unwanted duplication and discus only the first one. Let me add that the   
   comments in the code do not really clarify what you are asking for.   
   Especially it is unclear why some parts of the code are commented. I   
   assume that should be uncommented and that your question refers to the   
   uncommented code parts. Please improve this situation the next time.   
      
   > #include    
   >   
   > using namespace std;   
   >   
   > class A {   
   > public:   
   >       A() { i = 5; }   
   >   
   >       A copy() {   
   >           A a;   
   >           return a;       //by value!   
   >       }   
   >   
   >       int i;   
   > };   
   >   
   > class B : public A {   
   > public:   
   >       B() { this->i = 0; }   
   >       //B copy() {   
   >       //    //return a copy of A and   
   >       //    //dereference its address as an object ob B   
   >       //    return *static_cast( &A::copy() );  //<-- error issued   
   > here   
   >       //}   
      
   The error is expected and required, because the standard requires that   
   you cannot take the address of an rvalue, see [expr.unary.op] p3:   
      
   "The result of the unary & operator is a pointer to its operand. The   
   operand shall be an lvalue or a qualified-id."   
      
   A::copy() cannot be a qualified-id, but is an prvalue, therefore forming   
   the address of that is ill-formed.   
      
   >       B copy_2() {   
   >           A tmp = A::copy();   
   >           return  *(B*)&tmp;   
   >       }   
      
   This is ok: tmp is an lvalue and the address can be taken as described   
   above.   
      
   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