Re: basic_string ctor
- From: Jason Winnebeck <gillius@xxxxxxxxxxxxxxxxx>
- Date: Thu, 19 May 2005 14:19:35 -0400
Carl Daniel [VC++ MVP] wrote:
But here we're converting from a pointer to an object. The validity of that pointer is entirely up to the object's constructor to decide. I'm sure 10's of 1000's of C++ programmers have designed classes that take a pointer paramter to their constructor and provide some reasonable default behavior if the pointer is null. It's really not that radical an idea.
As shown in answer to Duane, you can make a valid pointer to an empty string and a NULL pointer both construct a std::string as empty.
But if you fold this distinction in std::string's constructor, so NULL will silently construct an empty std::string, then in the situation where the pointer being NULL is an error in the program, this will silently pass over this.
Yep. And in the 1000's of cases where the 0 isn't an error it'll frustrate and confuse the programmer when the seemingly reasonable std::string(0) crashes their program.
I don't consider this reasonable at all. In fact I can't think of a time where I've ever written code that might possibly pass null into a string method, nor code that had to explicitly check for null first. I can think of a few times you might want to convert a NULL to a std::string, but IMO those cases are all moot because you want to differentiate between an empty string and a null string. If std::string supported a "null" string value, maybe it would make more sense but I've never been a fan of "magical states".
Neither solution is perfect, and opinions vary as to which is "better" or "safer". Most opinions I've encountered agree that C++ would be easier to learn and safer for most programmers to use if there was less undefined behavior.
I agree with this, which is why I agree with Java's philosophies. What I disagree with is that string should allow nulls. No function should allow null unless there is a good reason or at least a very well-definined and intuitive reason to allow nulls (of which there are quite a few, but a lot less often than people would think).
If anything I'd want the std::string ctor to have "defined" over "undefined" behavior but I'd want that definiton to be an exception thrown (which basically guarantees your program will crash).
Jason .
- References:
- basic_string ctor
- From: John
- Re: basic_string ctor
- From: Carl Daniel [VC++ MVP]
- Re: basic_string ctor
- From: Duane Hebert
- Re: basic_string ctor
- From: Tom Widmer
- Re: basic_string ctor
- From: Carl Daniel [VC++ MVP]
- Re: basic_string ctor
- From: Stephen Howe
- Re: basic_string ctor
- From: Carl Daniel [VC++ MVP]
- basic_string ctor
- Prev by Date: Re: basic_string ctor
- Next by Date: Re: vector<bool> iterator bug in VC8 ?
- Previous by thread: Re: basic_string ctor
- Next by thread: Re: basic_string ctor
- Index(es):
Relevant Pages
|
Loading