Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#6716 closed Bugs (invalid)

Unexpected behaviour when constructing iterator_range from string literal

Reported by: Olaf van der Spek <olafvdspek@…> Owned by: Neil Groves
Milestone: To Be Determined Component: range
Version: Boost 1.48.0 Severity: Problem
Keywords: Cc:

Description

This behaviour is unexpected. Either don't provide automatic construction from string literals or ensure it works as expected.

#include <boost/range/iterator_range.hpp>
#include <string>

int main()
{
	std::string s = "Olaf";
	boost::iterator_range<const char*> r(s.data(), s.data() + s.size());
	assert(r == s);
	assert(r == "Olaf");
	return 0;
}

Change History (2)

comment:1 by Neil Groves, 11 years ago

Resolution: invalid
Status: newclosed

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.

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.

When compared to the const char[5] literal "Olaf\0" this is handled as any array would be. Since the size of the array is different it is not considered equal.

When dealing with literal strings it is documented that one should use as_literal. http://www.boost.org/doc/libs/1_49_0/libs/range/doc/html/range/reference/concept_implementation/semantics/functions.html

comment:2 by Olaf van der Spek <olafvdspek@…>, 11 years ago

Hi Neil,

When compared to the const char[5] literal "Olaf\0" this is handled as any array would be.

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?

Note: See TracTickets for help on using tickets.