Scott Meyers
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.
The difference is that there are no special cases in the specification of std::string to cover the default-constructed case. The other two may fall within the invariants, but they're degenerate objects requiring special consideration. -- Dave Abrahams Boost Consulting www.boost-consulting.com