Boost logo

Boost :

From: Andy Little (andy_at_[hidden])
Date: 2006-01-30 07:44:53


"Reece Dunn" <msclrhd_at_[hidden]> wrote
> Andy Little wrote:
>>The other surprise for me is that its possible to modify the capacity of a
>>fixed_string.
>
> Are you sure? You can write algorithms that don't rely on a specific
> capacity string.

OK I was under the impression you could (modify the capacity). Spell it out for
me in the documentation in this case.

> Thus, instead of:
>
> template< int n >
> int strlen( const fixed_string< n, char >& str )
> {
> return str.length();
> }
>
> you can write:
>
> int strlen( const fixed_string_base< char >& str )
> {
> return str.length();
> }

Its possible to do this for fixed_string directly too.

#include <boost/fixed_string/fixed_string.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/utility/enable_if.hpp>
#include <string>

namespace boost{

    template <typename T>
    struct is_fixed_string : mpl::false_{};

    template <int N, typename C, typename P1, typename P2>
    struct is_fixed_string<fixed_string<N,C,P1,P2> > : mpl::true_{};

    template <typename T>
    typename boost::enable_if<
        is_fixed_string<T>,
        int
>::type
    strlen( const T& str )
    {
      return str.length();
    }
}

int main()
{
    // check doesnt interfere here
    const char arr[]= "hello";
    std::cout << strlen(arr) <<'\n';

    boost::fixed_string<20> str = "hello";
    std::cout << strlen(str);

    int n =9;
    // std::cout << strlen(n); //error because not a fixed_string or char array
  }

>
>>That means the class name fixed_string is misleading. Unless fixed
>>means 'not broken' string implying that std::string is flawed.
>
> The std::string interface has several flaws, such as it having too many
> methods (see Herb Sutter's comments on this topic) and string types that
> have different CharTraits not being compatible with each other. However,
> fixed_string does not attempt to fix these. It is short for "fixed capacity
> string".
>
>>Its that sense
>>in which it doesnt promise what it delivers. I would expect a fixed_string
>>to
>>have a fixed length.
>
> It has a fixed capacity, but variable length.

OK.

[...]

> strlen example (and the rationale behind it) was that some users
> (during the initial development) wanted to be able to write operations that
> used fixed-capacity string such that they could redistribute the binaries
> only. Thus the use of the fixed_string_base. An earlier reviewer has shown
> me how to split these so you get the performance from using fixed_string and
> the ability to operate on any fixed_string< n > object without templatizing
> the method on n.

OK .

[...]

> The Spirit docs are very good :).

[...]

OK thats a good model . fixed_string should be easier to describe than Spirit
too.

> I intend to keep the behaviour of the fixed_string class (modified,
> according to the review feedback). This is what the unit tests are for :).
> It is just *how* that is implemented that will change.

What about the (lack of) overrun policy?

regards
Andy Little


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk