Boost C++ Libraries: Ticket #6716: Unexpected behaviour when constructing iterator_range from string literal https://svn.boost.org/trac10/ticket/6716 <p> This behaviour is unexpected. Either don't provide automatic construction from string literals or ensure it works as expected. </p> <pre class="wiki">#include &lt;boost/range/iterator_range.hpp&gt; #include &lt;string&gt; int main() { std::string s = "Olaf"; boost::iterator_range&lt;const char*&gt; r(s.data(), s.data() + s.size()); assert(r == s); assert(r == "Olaf"); return 0; } </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/6716 Trac 1.4.3 Neil Groves Sun, 15 Apr 2012 08:27:04 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/6716#comment:1 https://svn.boost.org/trac10/ticket/6716#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> This report is invalid. There is not an automatic construction from string literals. Historically many versions ago, there was an attempt to handle string literals seamlessly. This proved to not work well. </p> <p> Your example is an iterator_range explicitly constructed to be of length 4 ie. the null terminator has been removed from the const char* range. </p> <p> When compared to the const char<a class="changeset" href="https://svn.boost.org/trac10/changeset/5" title="Boost customization">[5]</a> literal "Olaf\0" this is handled as any array would be. Since the size of the array is different it is not considered equal. </p> <p> When dealing with literal strings it is documented that one should use as_literal. <a href="http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/concept_implementation/semantics/functions.html">http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/concept_implementation/semantics/functions.html</a> </p> Ticket Olaf van der Spek <olafvdspek@…> Sun, 15 Apr 2012 08:35:23 GMT <link>https://svn.boost.org/trac10/ticket/6716#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/6716#comment:2</guid> <description> <p> Hi Neil, </p> <blockquote class="citation"> <p> When compared to the const char<a class="changeset" href="https://svn.boost.org/trac10/changeset/5" title="Boost customization">[5]</a> literal "Olaf\0" this is handled as any array would be. </p> </blockquote> <p> That's the core of the problem. I did not expect this string literal to be handled as an array. Wouldn't it be safer to disallow implicit conversion of arrays and require the user to use as_array or as_literal to explicitly state what conversion is desired? </p> </description> <category>Ticket</category> </item> </channel> </rss>