Re: basic_string ctor
- From: "Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@xxxxxxxxxxxxxxx>
- Date: Thu, 19 May 2005 06:48:57 -0700
Stephen Howe wrote:
>> They're not the same in C, which has no real concept of a string.
>
> It most certainly does.
> Admittedly it has limitations (can't have a string of '\0''s,
> strlen() is O(N) not O(1), all memory managment of it is manual) but
> it has been like that since the early days.
Which doesn't make it right - C strings are lame at best and we all know it.
>
>> In the (IME far more common) case where the distinction is merely an
>> artifact of C's poor string concept, I'd prefer the C++ library to
>> simply accept 0 as an empty string.
>
> It has nothin to do with C's poor string concept.
> It has everything to do with the fact that NULL or 0 in pointer
> situations is regarded as an invalid pointer for all types.
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.
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.
-cd
.
- Follow-Ups:
- Re: basic_string ctor
- From: Jason Winnebeck
- Re: basic_string ctor
- From: Stephen Howe
- Re: basic_string ctor
- 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
- 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