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 31,577 of 33,346   
   Ulrich Eckhardt to nikkoara   
   Re: Initialization of reference from imp   
   17 Oct 11 23:48:10   
   
   05e2b656   
   From: doomster@knuut.de   
      
   nikkoara wrote:   
   > I am inclined to believe the following is a compiler error. Any   
   > opinions?   
   >   
   > $ g++ --version | head -n 1; cat -n t.cpp; g++ -c t.cpp   
   > g++ (GCC) 4.6.1   
   >      1  struct B { };   
   >      2  struct D : B { } d;   
   >      3   
   >      4  struct X { };   
   >      5  struct Y : X {   
   >      6      Y (D const&);   
   >      7  };   
   >      8   
   >      9  X const& ref = d;   
   > t.cpp:9:16: error: invalid initialization of reference of type ‘const   
   > X&’ from expression of type ‘D’   
      
   I wouldn't consider that a fault. The question, looking at the last line,   
   is why should the compiler consider the side-trip through a 'Y' for   
   initialisation of 'ref'? There could be a limitless number of classes that   
   derive from 'X' and the code doesn't explicitly or implicitly specify one.   
      
      
   > This slightly changed version compiles:   
   >   
   > $ g++ --version | head -n 1; cat -n t2.cpp; g++ -c t2.cpp   
   > g++ (GCC) 4.6.1   
   >      1  struct X { };   
   >      2  struct Y : X { };   
   >      3   
   >      4  struct B { };   
   >      5  struct D : B {   
   >      6      operator Y();   
   >      7  } d;   
   >      8   
   >      9  X const& ref = d;   
      
   Here, the number of conversions is limited to the bases of 'd' and the   
   defined conversion operators, so no code external to this could change the   
   meaning of this code.   
      
      
   Uli   
      
      
   --   
         [ 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