Article View: comp.std.c++
Article #1010Initialization ambiguities
From: jimc
Date: Wed, 10 Apr 1991 19:02
Date: Wed, 10 Apr 1991 19:02
58 lines
3367 bytes
3367 bytes
Two possible ambiguities about initialization in Ellis & Stroustrup,
"The Annotated C++ Reference Manual". First, suppose class A is an
aggregate (sect 8.4.1) containing one data member, an array of V's,
which are also aggregates.
struct V { float data[2]; }
struct A { V array[2]; }
A wrong = { {1, 2}, {3, 4} }; //{3,4} inits 2nd data member
//which does not exist
V v0 = {1, 2};
V v1 = {3, 4};
A ambig = { { v0, v1 } }; //What does v0 init?
//Could be ambig.array[0], could be ambig.array[0].data[0]
When an aggregate is initialized from a list of aggregates, and when one
of these matches a sub-aggregate, is it copied or does it (disastrously)
initialize members of the sub-aggregate? The book can be read either way.
What I really wanted was the effect shown above as "wrong", just as a
traditional "C" array would be initialized.
Second ambiguity: a derived class has a constructor from a reference to its
base class. Is this a copy constructor? Specifically:
struct Base {
Base(args); //No explicit copy constructor here
};
struct Derv : public Base {
Derv(const Base& b); //Pseudo-copy constructor
};
Derv& invert(const Base& b) {
Derv result(b);
/* thrash about */
return result; //Using which copy constructor?
}
Reading in sect. 12.6.1 "Explicit Initialization" p. 288: "...function
return is equivalent to Derv dest = result". p. 284: "This value is
used as the argument to a copy constructor". Sect. 5.3.3 "New" p. 61:
"Access and ambiguity control are done for both operator new() and the
constructor". It all implies very strongly that a constructor should be
sought that will swallow a Derv, which the provided constructor will
do. Nonetheless my compiler demanded an exact match on argument type,
chucked my constructor, and generated a bitwise copy (whereupon on
destruction a free pointer was freed again, the heap was smeared,
MS-DOS was smeared, my disc was smeared, and matters went downhill from
there. Fortunately I make backups.)
An authoritative statement would be appreciated in the evolving standard
of whether a copy constructor can have non-exact matching arguments.
If you post a reply, I would appreciate a mailed copy since my news machine
has a very short expiration time.
James F. Carter (213) 825-2897
UCLA-Mathnet; 6221 MSA; 405 Hilgard Ave.; Los Angeles, CA, USA 90024-1555
Internet: jimc@math.ucla.edu BITNET: jimc%math.ucla.edu@INTERBIT
UUCP:...!{ucsd,ames,ncar,gatech,purdue,rutgers,decvax,uunet}!math.ucla.edu!jimc
James F. Carter (213) 825-2897
UCLA-Mathnet; 6221 MSA; 405 Hilgard Ave.; Los Angeles, CA, USA 90024-1555
Internet: jimc@math.ucla.edu BITNET: jimc%math.ucla.edu@INTERBIT
UUCP:...!{ucsd,ames,ncar,gatech,purdue,rutgers,decvax,uunet}!math.ucla.edu!jimc
Technical Details
Message-ID:
<1991Apr10.190211.29678@math.ucla.edu>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ucla-cs!ucla-ma!jimcShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!swrinde!elroy.jpl.nasa.gov!ucla-cs!ucla-ma!jimc From: jimc@julia.math.ucla.edu Newsgroups: comp.std.c++ Subject: Initialization ambiguities Keywords: initialization initializers aggregate copy Message-ID: <1991Apr10.190211.29678@math.ucla.edu> Date: 10 Apr 91 19:02:11 GMT Sender: jimc@MATH.UCLA.EDU Reply-To: jimc@MATH.UCLA.EDU (Jim Carter) Organization: UCLA Mathematics Department Lines: 58