
Peter Dimov wrote:
It would be perfectly reasonable for a language-level foreach to omit the trailing zero when iterating over a character literal. But I don't think that this case is possible to detect. I wouldn't be surprised if it is, though, given the current wizardry in BOOST_FOREACH (somehow exploiting the fact that literals are const but convertible to char*.)
It does? What makes you think that? I wonder if the fact that a const char[N] is convertible to a (non-const) char* can be used to detect whether a given character array should be treated as a null-terminated string. It's an interesting question, but I don't think it's a good idea, becuase it wouldn't handle cases like: const char sz[] = "hello"; boost::size(sz); // 5 or 6? boost::size("hello"); // better give the same answer as above FWIW, I agree with Peter and Dave A. that character arrays should be treated as arrays and not as null-terminated strings. The truth is, you just don't know, and you need the user's guidance to pick. I'd like to see something like: template<typename T, std::size_t N> iterator_range<T const *> ignore_trailing_null(T const (&rg)[N]) { BOOST_ASSERT(T(0) == rg[N-1]); return make_iterator_range(&rg[0], &rg[N-1]); } That way: boost::size("hello"); // 6 boost::size(ignore_trailing_null("hello")); // 5 Perhaps there is a better is a better name than "ignore_trailing_null". -- Eric Niebler Boost Consulting www.boost-consulting.com