Issue Details (XML | Word | Printable)

Key: STDCXX-78
Type: Improvement Improvement
Status: Open Open
Priority: Minor Minor
Assignee: Unassigned
Reporter: Martin Sebor
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
C++ Standard Library

std::copy() doesn't detect overlapping ranges

Created: 01/Dec/05 09:37 AM   Updated: 15/May/08 08:49 PM
Return to search
Component/s: 25. Algorithms
Affects Version/s: 4.1.2, 4.1.3, 4.1.4, 4.2.0, 4.2.1
Fix Version/s: 4.3.0

Time Tracking:
Original Estimate: 4h
Original Estimate - 4h
Remaining Estimate: 4h
Remaining Estimate - 4h
Time Spent: Not Specified
Remaining Estimate - 4h

Environment: all

Severity: Usability


 Description  « Hide
Quoting from the response to the following post:
http://mail-archives.apache.org/mod_mbox/incubator-stdcxx-dev/200511.mbox/%3c4D6A8407B7AC6F4D95B0E55C4E7C4C6202EEFD7E@exmsk.moscow.vdiweb.com%3e

-------- Original Message --------
Subject: Re: questions about the lib.alg.copy test
Date: Wed, 30 Nov 2005 17:33:10 -0700
From: Martin Sebor <sebor@roguewave.com>
To: stdcxx-dev@incubator.apache.org
References: <4D6A8407B7AC6F4D95B0E55C4E7C4C6202EEFD7E@exmsk.moscow.vdiweb.com>

Anton Pevtsov wrote:
[...]
>
> 2. The copy algorithm can work in case when the destination range
> overlaps the source range (of course, first position of the source range
> should not be contained in the destination range). Current version
> doesn't contain special test for this case, but I prefer to have such
> test. What do you think about it?

The requirement in 25.2.1, p3 is that "result shall not be in the
range [first, last)." The algorithm doesn't detect violations of
this requirement but it probably should in debug mode. This would
be a useful enhancement in general. Let me file an enhancement for
it.

Martin



 All   Comments   Work Log   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
Martin Sebor added a comment - 15/May/08 08:32 PM
This might be harder than it seemed. The implementation of the algorithm looks like this:
template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator res)
{
    _RWSTD_ASSERT_RANGE (first, last);

    for (; !(first == last); ++first,++res)
        *res = *first;
    return res;
}

The naive solution is to change it like so:

template <class InputIterator, class OutputIterator>
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator res)
{
    _RWSTD_ASSERT_RANGE (first, last);
    _RWSTD_ASSERT_NOT_IN_RANGE (res, first, last);

    for (; !(first == last); ++first,++res)
        *res = *first;
    return res;
}

But it doesn't work because a) there's no _RWSTD_ASSERT_NOT_IN_RANGE() and b) if there was (as a parallel to _RWSTD_ASSERT_RANGE), it would require some changes to the __rw_in_range() function template to make it work with cv-qualified pointers (i.e., in the common case when InputIterator=const T* and OutputIterator=T*).

All this might be worth doing if it can be used in other algorithms besides std::copy(). Otherwise it seems like a lot of effort and overhead for just one algorithm.


Martin Sebor added a comment - 15/May/08 08:49 PM
Let's see if this is worth doing in 4.3.

Martin Sebor made changes - 15/May/08 08:49 PM
Field Original Value New Value
Affects Version/s 4.1.3 [ 12310191 ]
Severity Usability
Affects Version/s 4.2.0 [ 12311945 ]
Affects Version/s 4.1.4 [ 12310693 ]
Affects Version/s 4.2.1 [ 12312690 ]
Fix Version/s 4.3 [ 12312692 ]
Remaining Estimate 4h [ 14400 ]
Original Estimate 4h [ 14400 ]