From -3713250171294921841
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f78e5,4441c5f6d7843223
X-Google-Attributes: gidf78e5,public
X-Google-ArrivalTime: 2002-08-16 05:36:03 PST
Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!canoe.uoregon.edu!logbridge.uoregon.edu!kibo.news.demon.net!news.demon.co.uk!demon!mail2news.demon.co.uk!not-for-mail
From: rmaddox@isicns.com (Randy Maddox)
Newsgroups: comp.std.c++
Subject: Re: C++0x Wish List (explicit class)
Date: Fri, 16 Aug 2002 12:35:08 GMT
Organization: http://groups.google.com/
Approved: Fergus Henderson <fjh@cs.mu.oz.au>, moderator of comp.std.c++
Message-ID: <8c8b368d.0208150528.2bc34601@posting.google.com>
References: <3D48C729.40503@spamnot.codeweavers.com> <ehdlkuo9gkcu69b6og2aup9o63knu7tpra@4ax.com> <8c8b368d.0208060921.43e9e9d5@posting.google.com> <aiphem$9e5$1@acs2.byu.edu> <3D589892.3050008@spamnot.codeweavers.com>
X-Trace: mail2news.demon.co.uk 1029501314 mail2news:3353 mail2news mail2news.demon.co.uk
X-Complaints-To: abuse@demon.net
X-Mail2News-Path: news.demon.net!mulga.cs.mu.oz.au
X-Authentication-Warning: mulga.cs.mu.OZ.AU: fjh set sender to devnull@stump.algebra.com using -f
X-Robomod: STUMP, ichudov@algebra.com (Igor Chudov)
Delivered-To: std-c++@ncar.ucar.edu
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
NNTP-Posting-Date: 15 Aug 2002 13:28:14 GMT
Lines: 83
Xref: archiver1.google.com comp.std.c++:13264

Ken Thomases <ken@spamnot.codeweavers.com> wrote in message news:<3D589892.3050008@spamnot.codeweavers.com>...
> 
> Is it true that providing a constructor causes suppression of 
> compiler-supplied destructor?  I don't think it is.  I just tried this 
> in CodeWarrior 8.1:
> 
> #include <string>
> #include <vector>
> 
> class MyTest
> {
> public:
>      MyTest() : m_string("blat"), m_vector(10) { }
>      MyTest(const MyTest& in_other) :
>          m_string(in_other.m_string), m_vector()
>          { m_vector.push_back(m_string); }
> 
> private:
>      const std::string m_string;
>      std::vector<std::string> m_vector;
> };
> 
> int main()
> {
>      MyTest foo;
>      MyTest bar(foo);
>      // MyTest gub;
>      // gub = foo;  // error
> }
> 
> 
> I figure those constructors have enough meat to be clearly non-trivial. 
>   The string is const to test suppression of compiler-supplied 
> assignment operator.  It compiles quite nicely.  Now, clearly, for this 
> code to be legal the compiler must be getting a destructor from 
> somewhere.  Right?  Neither object's m_string or m_vector are allowed to 
> leak, are they?
> 
> For kicks, I tried making MyTest privately inherit boost::noncopyable. 
> Still compiled, although the compiler error if I uncommented the gub 
> lines changed from "using implicit copy assignment for class with const 
> or reference member" to "illegal access to private/protected member".
> 
> With explicit class, no destructor would be provided.  The compiler 
> would produce errors for almost any instantiation of the class (except, 
> dynamically allocated on the heap and never deleted).  What would be the 
> value of this?  It would remind a programmer who suffered a lapse in 
> concentration that their complex class needed a programmer-supplied 
> destructor up to the task.  Better a compiler error than a 
> compiler-supplied destructor which masks the problem.
> 
> Ken
> 

Speaking of dtors, I would also point out that boost::noncopyable has
a non-virtual dtor, which is pretty unusual for a class intended as a
base class.

Now, I know that the usual idiom is to use private derivation so that
no reference or pointer to a class derived from boost::noncopyable
will be automatically converted to a reference or pointer to its base
class, but there is nothing to enforce this.  It's just a convention. 
If somehow, somewhere, someone does delete an object of a class
derived from boost::noncopyable using a pointer to its base class, the
correct dtor will not be used, and problems are likely.

Of course, this could easily be fixed by adding a virtual dtor, but I
would presume the reason it was left non-virtual in the first place is
to allow the "empty base class" optimization, which a vtable would
prevent.  Oops!  Maybe this solution isn't quite as perfect as we
might assume.

So tell me again how boost::noncopyable is better than extending the
use of an existing keyword in a sensible way?

Randy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]



