Override n + iterator operator in the iterator

This commit is contained in:
HenryLee 2017-05-27 13:44:14 +10:00
parent 52adf3fd5b
commit ed62129f8e
2 changed files with 33 additions and 2 deletions

View File

@ -8474,18 +8474,29 @@ class basic_json
@brief add to iterator @brief add to iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
iter_impl operator+(difference_type i) iter_impl operator+(difference_type i) const
{ {
auto result = *this; auto result = *this;
result += i; result += i;
return result; return result;
} }
/*!
@brief addition of distance and iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`.
*/
friend iter_impl operator+(difference_type i, const iter_impl& it)
{
auto result = it;
result += i;
return result;
}
/*! /*!
@brief subtract from iterator @brief subtract from iterator
@pre The iterator is initialized; i.e. `m_object != nullptr`. @pre The iterator is initialized; i.e. `m_object != nullptr`.
*/ */
iter_impl operator-(difference_type i) iter_impl operator-(difference_type i) const
{ {
auto result = *this; auto result = *this;
result -= i; result -= i;

View File

@ -269,6 +269,16 @@ TEST_CASE("iterators 2")
CHECK_THROWS_AS(it + 1, json::invalid_iterator); CHECK_THROWS_AS(it + 1, json::invalid_iterator);
CHECK_THROWS_WITH(it + 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators"); CHECK_THROWS_WITH(it + 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
} }
{
auto it = j_object.begin();
CHECK_THROWS_AS(1 + it, json::invalid_iterator);
CHECK_THROWS_WITH(1 + it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
}
{
auto it = j_object.cbegin();
CHECK_THROWS_AS(1 + it, json::invalid_iterator);
CHECK_THROWS_WITH(1 + it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
}
{ {
auto it = j_object.begin(); auto it = j_object.begin();
CHECK_THROWS_AS(it -= 1, json::invalid_iterator); CHECK_THROWS_AS(it -= 1, json::invalid_iterator);
@ -688,6 +698,16 @@ TEST_CASE("iterators 2")
CHECK_THROWS_AS(it + 1, json::invalid_iterator); CHECK_THROWS_AS(it + 1, json::invalid_iterator);
CHECK_THROWS_WITH(it + 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators"); CHECK_THROWS_WITH(it + 1, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
} }
{
auto it = j_object.rbegin();
CHECK_THROWS_AS(1 + it, json::invalid_iterator);
CHECK_THROWS_WITH(1 + it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
}
{
auto it = j_object.crbegin();
CHECK_THROWS_AS(1 + it, json::invalid_iterator);
CHECK_THROWS_WITH(1 + it, "[json.exception.invalid_iterator.209] cannot use offsets with object iterators");
}
{ {
auto it = j_object.rbegin(); auto it = j_object.rbegin();
CHECK_THROWS_AS(it -= 1, json::invalid_iterator); CHECK_THROWS_AS(it -= 1, json::invalid_iterator);