Topic: basic_string<C,T> class invariant


Author: steagall@deltalogic.com (Bob Steagall)
Date: 1997/05/03
Raw View
Is the following a class invariant of basic_string<C,T>:

    for some instance S of basic_string<C,T>,

    S.length() == T::length(S.c_str())

In other words, are basic_string's allowed to have embedded characters
equal to T::eos()?

If so, why?

As far as I can tell, this issue is not specifically addressed in
Chapter 21.

--Bob
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Matt Austern <austern@isolde.mti.sgi.com>
Date: 1997/05/04
Raw View

steagall@deltalogic.com (Bob Steagall) writes:

> Is the following a class invariant of basic_string<C,T>:
>
>     for some instance S of basic_string<C,T>,
>
>     S.length() == T::length(S.c_str())
>
> In other words, are basic_string's allowed to have embedded characters
> equal to T::eos()?
>
> If so, why?
>
> As far as I can tell, this issue is not specifically addressed in
> Chapter 21.

No, that is not a class invariant of basic_string<>.  Equivalently:
yes, strings are allowed to have embedded null characters in them.
Perhaps there should be a footnote added to clause 21; on the other
hand, I don't think there's really anything in clause 21 that would
lead you to think that null characters are special in any way.

As for why...  Well, you might turn that around and ask what's so
special about null characters; nothing, really.  The eos() character
is really used by basic_string<> in only one place: conversions
between basic_string<> and C style null-terminated arrays of
characters.

Strings are basically just sequences of characters.  There can be null
characters in a vector<char>, and the same is true of
basic_string<char>.
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: petebecker@aol.com (PeteBecker)
Date: 1997/05/04
Raw View

In article <5kfi90$8lj$1@nerd.apk.net>, steagall@deltalogic.com (Bob
Steagall) writes:

>Is the following a class invariant of basic_string<C,T>:
>
>    for some instance S of basic_string<C,T>,
>
>    S.length() == T::length(S.c_str())

No.

>
>In other words, are basic_string's allowed to have embedded characters
>equal to T::eos()?

Yes.

>
>If so, why?

Two reasons. First, it is sometimes useful to be able to handle sequences
that contain embedded nuls. Second, nul termination is an implementation
detail that should not be exposed in a class interface. There are no magic
characters in the string class, except where necessary to properly
interact with C-style strings. The internal representation of the string
class is not required to be nul terminated. This allows implementors more
flexibility.
    -- Pete
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]