Article View: comp.std.c++
Article #2651Re: Is Derived::Derived(const Base&) a copy ctor?
From: Pablo Halpern
Date: Mon, 20 Nov 1995 00:03
Date: Mon, 20 Nov 1995 00:03
58 lines
3157 bytes
3157 bytes
wil@ittpub.nl (Wil Evers) wrote:
>The ARM, section section 12.1, says:
>
>"A copy constructor for a class X is a constructor that can be called to
>copy an object of class X; that is, one that can be called with a single
>argument of type X."
>
...
>In contrast, the DWP says (12.8.2, [class.copy])
>
>"A constructor for class X is a copy constructor if its first parameter is
>of type X& or const X&, and either there are no other parameters or all
>other parameters have default arguments."
>
...
>Is this a deliberate change from the definition in the ARM? If so, does
>anyone know what the rationale behind this change is? (Personally, I think
>the definition in the DWP is preferable because I occasionally need to
>write such a constructor to `upgrade' a base class object to a derived
>class object and I got bitten once or twice because the compiler wouldn't
>generate a copy constructor for the derived class if I did so. Still, I
>can also imagine code that would break because of this change).
Actually, I prefer the ARM version specifically because of the issue we
are discussing in the thread about auto_ptr<>. If you want the argument
to your copy constructor going through some sort of intermediate proxy
conversion, then you don't want that work undone by the compiler:
template <class T>
class auto_ptr<T>
{
class proxy { ... };
...
operator proxy(); // conversion to proxy<T>
auto_ptr<T>(const proxy&); // line 3
...
};
According to the ARM, line 3 declares a copy constructor because you can
call it with an auto_ptr<T> argument. The compiler will not generate a
copy constructor for you. This is good, in this case. But according to
the DWP, line 3 does NOT declare a copy constructor and all of your
lovely work is lost as the compiler generates its own (incorrect) copy
constructor. Declaring a private copy constructor does not help because
the compiler still sees the copy constructor and just makes it illegal
to try to use it.
I can always manually make a copy constructor that does the same thing
that the compiler-generated one would do, but I can't always suppress
the compiler's urge to do something for me.
-------------------------------------------------------------
Pablo Halpern phalpern@truffle.ultranet.com
[ comp.std.c++ is moderated. Submission address: std-c++@ncar.ucar.edu.
Contact address: std-c++-request@ncar.ucar.edu. The moderation policy
is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID:
<48oe09$as0@caesar.ultra.net>Path:
arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamageReferences:
<1995Oct30.145329.1625@ittpub>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!engnews1.Eng.Sun.COM!taumet!clamage From: phalpern@truffle.ultranet.com (Pablo Halpern) Newsgroups: comp.std.c++ Subject: Re: Is Derived::Derived(const Base&) a copy ctor? Date: 20 Nov 1995 00:03:52 GMT Organization: UltraNet Communications, Inc. Approved: clamage@eng.sun.com (comp.std.c++) Message-ID: <48oe09$as0@caesar.ultra.net> References: <1995Oct30.145329.1625@ittpub> NNTP-Posting-Host: taumet.eng.sun.com Content-Type: text X-Nntp-Posting-Host: truffle.ultranet.com X-Newsreader: Forte Agent .99b.113 Content-Length: 2299 Originator: clamage@taumet