
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.
No, a singular iterator is not a valid object and it fulfills no invariants. You can't even copy it without UB. The ofstream can be made to fulfill its invariant if we define its invariant as "true", I guess, or something with a similar utility. A default-constructed std::string is fine. You are not breaking the invariant with invoking op[], you are supplying an invalid index to it. BTW, did you know that the const version of op[] is required to return 0 for s[0] :-) How exactly is it supposed to return 0 when the return type is a char const& is left as an exercise for the implementer.