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,817 of 33,346    |
|    =?windows-1252?Q?Daniel_Kr=FCgler?= to All    |
|    Re: C'tor vs. function declaration    |
|    19 Jan 12 02:38:03    |
   
   f1105031   
   From: daniel.kruegler@googlemail.com   
      
   Am 18.01.2012 21:12, schrieb Michael Smolensky:   
   > Consider an example:   
   >   
   > struct S   
   > {   
   > S( int ) {}   
   > };   
   >   
   > class C   
   > {   
   > public:   
   > C() {}   
   > C( int ) {}   
   > C( S ) {}   
   >   
   > int get_int() { return 0; }   
   > };   
   >   
   > int main()   
   > {   
   > C obj0;   
   >   
   > int i = 0;   
   >   
   > C obj1( S( obj0.get_int() ) ); // (1) c'tor C( int )   
   > C obj2( S( i ) ); // (2) function decl C ()( S )   
   > C obj3( (S( i )) ); // (3) c'tor C( int )   
   > C obj4( *&S( i ) ); // (4) c'tor C( int )   
   >   
   > obj0 = obj1; // (5) OK   
   > obj0 = obj2; // (6) error: cannot convert   
   > function ptr to int   
   > obj0 = obj3; // (7) OK   
   > obj0 = obj4; // (8) OK   
   >   
   > return 0;   
   > }   
   >   
   > Both GCC 4.1 and VC9 treat line (2) as a function declaration, not a   
   > constructor C( S ). Workarounds like (3) and (4) work as expected. Can   
   > someone explain?   
      
   Please read the FAQ for this:   
      
   http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.21   
      
   I wonder that line (4) is accepted, it shouldn't: S( i ) creates an   
   rvalue and the expression &S( i ) would attempt to get the address of   
   it. This should be ill-formed according to 5.3.1 [expr.unary.op] p3,   
   only lvalues (or a qualified-id) are feasible operands of the address   
   operator.   
      
   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