Boost logo

Boost :

From: Eric Niebler (eric_at_[hidden])
Date: 2007-01-10 13:20:49


Consider the output of the following program:

     #include <iostream>
     #include <algorithm>
     #include <boost/range.hpp>
     #include <boost/range/as_array.hpp>
     #include <boost/range/as_literal.hpp>

     void print_char(char ch)
     {
         std::cout << ch << '\t' << (int)ch << '\n';
     }

     template<typename Range>
     void print_range(Range const &rng)
     {
         std::for_each(boost::begin(rng), boost::end(rng), print_char);
     }

     int main()
     {
         print_range(boost::as_literal("hello"));
         std::cout << '\n';

         print_range(boost::as_array("hello"));
         std::cout << '\n';

         return 0;
     }

It prints the following:

     h 104
     e 101
     l 108
     l 108
     o 111

     h 104
     e 101
     l 108
     l 108
     o 111

There is no difference between treating a string literal as an array or
as a null-terminated string. There should be. When treated as an array,
the null-terminator should be treated as any other element in the array.

The problem comes from range/detail/implementation_help.hpp:

     template< class T, std::size_t sz >
     inline T* array_end( T BOOST_RANGE_ARRAY_REF()[sz],
char_or_wchar_t_array_tag )
     {
         return boost_range_array + sz - 1;
     }

It seems Boost.Range is still treating arrays of char and wchar_t
specially. IIRC, we decided long ago that it shouldn't.

This is on HEAD, BTW.

-- 
Eric Niebler
Boost Consulting
www.boost-consulting.com

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