Boost logo

Boost :

Subject: Re: [boost] [lexical_cast] Fast conversion from boost::iterator_range<std::string::iterator>?
From: Olaf van der Spek (ml_at_[hidden])
Date: 2012-01-26 12:43:06


On Thu, Jan 26, 2012 at 6:14 PM, Michel Morin <mimomorin_at_[hidden]> wrote:
> IIUC, the main concern to implement optimized code is that
> how to obtain begin and end pointer to CharT from `iter_rng` safely.
> With C++11-conforming standard library, it is safe to implement
>
>    bool operator<<(::boost::iterator_range<std::string::iterator> const& str)
>    {
>        if (!str.empty()) {
>            start = const_cast<CharT*>(&str.front());
>            finish = start + str.size();
>        }
>        else {
>            start = 0;
>            finish = 0;
>        }
>        return true;
>    }
>
> because `&*(s.begin() + n) == &*s.begin() + n` (`s` is an object of
> std::basic_string<…>)
> is guaranteed.
>
> But, in C++03, the above code is not safe; the standard does not guarantee that
> std::basic_string<…> is stored contiguously (though many standard library
> implementations store it contiguously).

What about using c_str() instead of begin()? It does provide that
guarantee in C++03.

-- 
Olaf

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