[range] begin()/end() for zero terminated strings

Can anyone tell why boost 1.35 dropped support of boost::begin()/end() for zero terminated strings (const char*/wchar_t*)? Raider

on Wed Jun 18 2008, Raider
Can anyone tell why boost 1.35 dropped support of boost::begin()/end() for zero terminated strings (const char*/wchar_t*)?
Because it was evil in generic code. An array of char was interpreted as being as long as the initial sequence of nonzero elements, but an array of anything else was interpreted as being as long as the array. You might ask why we didn't make it so that char(&)[N] was interpreted as an array of length N but char* was interpreted as a null-terminated string that only has forward iterators (to avoid O(N) "random access" operations, which are also evil in generic code)... I'm not entirely sure of the answer to that. -- Dave Abrahams BoostPro Computing http://www.boostpro.com

David Abrahams wrote:
on Wed Jun 18 2008, Raider
wrote: Can anyone tell why boost 1.35 dropped support of boost::begin()/end() for zero terminated strings (const char*/wchar_t*)?
Because it was evil in generic code. An array of char was interpreted as being as long as the initial sequence of nonzero elements, but an array of anything else was interpreted as being as long as the array.
You might ask why we didn't make it so that char(&)[N] was interpreted as an array of length N but char* was interpreted as a null-terminated string that only has forward iterators (to avoid O(N) "random access" operations, which are also evil in generic code)... I'm not entirely sure of the answer to that.
Actually, the support was not dropped altogether. It was just made explicit instead of implicit. You need to use as_literal/as_array helpers if you want to dealy with char*/char[]/wchar_t*/wchar_t[] as ranges. Best Ragards, Pavol

Can anyone tell why boost 1.35 dropped support of boost::begin()/end() for zero terminated strings (const char*/wchar_t*)?
Because it was evil in generic code. An array of char was interpreted as being as long as the initial sequence of nonzero elements, but an array of anything else was interpreted as being as long as the array.
You might ask why we didn't make it so that char(&)[N] was interpreted as an array of length N but char* was interpreted as a null-terminated string that only has forward iterators (to avoid O(N) "random access" operations, which are also evil in generic code)... I'm not entirely sure of the answer to that.
Actually, the support was not dropped altogether. It was just made explicit instead of implicit. You need to use as_literal/as_array helpers if you want to dealy with char*/char[]/wchar_t*/wchar_t[] as ranges.
Pavol, great thanks for info!

David Abrahams skrev:
on Wed Jun 18 2008, Raider
wrote: Can anyone tell why boost 1.35 dropped support of boost::begin()/end() for zero terminated strings (const char*/wchar_t*)?
You might ask why we didn't make it so that char(&)[N] was interpreted as an array of length N but char* was interpreted as a null-terminated string that only has forward iterators (to avoid O(N) "random access" operations, which are also evil in generic code)... I'm not entirely sure of the answer to that.
We believed that support for null-terminated strings would belong in string focused libraries. The function as_literal() was added for that purpose (so just use this function if you use BOOST_FOREACH with char* objects). I guess we could define the iterator type of char* to be some forward iterator, but I'm not sure how useful that really is. -Thorsten

David Abrahams wrote:
on Wed Jun 18 2008, Raider
wrote: Can anyone tell why boost 1.35 dropped support of boost::begin()/end() for zero terminated strings (const char*/wchar_t*)?
Because it was evil in generic code. An array of char was interpreted as being as long as the initial sequence of nonzero elements, but an array of anything else was interpreted as being as long as the array.
You might ask why we didn't make it so that char(&)[N] was interpreted as an array of length N but char* was interpreted as a null-terminated string that only has forward iterators (to avoid O(N) "random access" operations, which are also evil in generic code)... I'm not entirely sure of the answer to that.
I've got the problem. It's not possible to correctly distinguish null-terminated strings and straight arrays... May be the solution is to make something like strbegin()/strend() and include it to the Boost.StringAlgo library. Or... proxy container like this: template <typename Char> class NullTerminatedStringRange { NullTerminatedStringRange(Char*); Char* begin(); Char* end(); }
participants (4)
-
David Abrahams
-
Pavol Droba
-
Raider
-
Thorsten Ottosen