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,964 of 33,346   
   =?ISO-8859-1?Q?Daniel_Kr=FCgler?= to Kris Prad   
   Re: Incomplete class with auto_ptr   
   02 Mar 12 02:35:24   
   
   464b92e4   
   From: daniel.kruegler@googlemail.com   
      
   On 2012-02-29 12:58, Kris Prad wrote:   
   > This code snippet results in compiler warning:   
   >   
   > ----   
   > #include   
   > class A; // fwd class decl.   
   >   
   > void test()   
   > {   
   >       A* a;   
   >       std::auto_ptr  ap(a); // A is not defined here   
   > }   
      
   This code is non-conforming. std::auto_ptr requires (implied by the   
   general library requirements) that the template argument must be a   
   complete type.   
      
   > On Comeau online compiler:   
   > "memory", line 76: warning: delete of pointer to incomplete class   
   >       ~auto_ptr() { delete _M_ptr; }   
   >   
   > My question:   
   > Eventually ~auto_ptr() must see the underlying class definition for   
   > the code to work correctly. So, why is this only a warning, and not a   
   > full fledged error?   
      
   Note that there is no fundamental need for a constructor to see the   
   complete type. The language explicitly describes when this is   
   well-defined (The short version is: If the type has a non-trivial   
   destructor or a class-specific deallocation function).   
      
   If you can, use std::unique_ptr (from C++1): It is specified to make the   
   code ill-formed (including diagnostic), if the template parameter is   
   incomplete, when std::unique_ptr's destructor is instantiated.   
      
   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