Boost logo

Boost Users :

From: Rene Rivera (grafikrobot_at_[hidden])
Date: 2006-09-14 02:15:10


Scott Meyers wrote:
> Marshall Clow wrote:
>> At 9:06 PM -0700 9/13/06, Scott Meyers wrote:
>> [ snip ]
>>> I'll note that C++ itself allows "uninitialized" objects with
>>> constructors to be created all the time:
>>>
>>> std::ofstream ofs;
>>> std::vector<int>::iterator i;
>>> std::string s;
>> Just a nit - I think that your third example is not like the others.
>> A std::string, AFAIK, constructed with the default constructor, is
>> perfectly valid - just empty.
>
> In each case, a default constructor is invoked. They're all valid
> objects that presumably fulfill their invariants, it's just that you
> can't safely invoke very many operations on them. On the string s, for
> example, invoking size is fine, but invoking operator[] is not fine at all.

But that's true for string even with valid non-default construction:

std::string s("");

And also true, in possibly invoking erroneous behavior, of all C++
arrays and array like objects. It's not a "defect" of the default
construction of string. But a "defect" of the string interface. Given a
differently designed interface, that did not have the operator[], would
not have that "defect".

Note, I'm not suggesting that this would, by inference, be extended to
two phase construction designs. But then again, this is a matter of
perspective. If one considers the construction to determine the
allowable interface, then your string example is apropos.

To me that suggest implementing, when required, two phase designs with
double construction. For example:

const_string s0;
string s1(s0+"something");

But that looks hellishly complicated to pull off design wise.

-- 
-- Grafik - Don't Assume Anything
-- Redshift Software, Inc. - http://redshift-software.com
-- rrivera/acm.org - grafik/redshift-software.com
-- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net