Movatterモバイル変換


[0]ホーム

URL:



This page is a snapshot from the LWG issues list, see theLibrary Active Issues List for more information and the meaning ofCD1 status.

187. iter_swap underspecified

Section: 26.7.3[alg.swap]Status:CD1Submitter: Andrew KoenigOpened: 1999-08-14Last modified: 2016-01-28

Priority:Not Prioritized

View all otherissues in [alg.swap].

View all issues withCD1 status.

Discussion:

The description of iter_swap in 25.2.2 paragraph 7,says that it``exchanges the values'' of the objects to which two iteratorsrefer.

What it doesn't say is whether it does so using swapor using the assignment operator and copy constructor.

Thisquestion is an important one to answer, because swap is specialized towork efficiently for standard containers.
For example:

vector<int> v1, v2;iter_swap(&v1, &v2);

Is this call to iter_swap equivalent to calling swap(v1, v2)? Or is it equivalent to

{vector<int> temp = v1;v1 = v2;v2 = temp;}

The first alternative is O(1); the second is O(n).

A LWG member, Dave Abrahams, comments:

Not an objection necessarily, but I want to point out the cost ofthat requirement:

iter_swap(list<T>::iterator, list<T>::iterator)

can currently be specialized to be more efficient thaniter_swap(T*,T*) for many T (by using splicing). Your proposal wouldmake that optimization illegal. 

[Kona: The LWG notes the original need for iter_swap was proxy iteratorswhich are no longer permitted.]

Proposed resolution:

Change the effect clause of iter_swap in 25.2.2 paragraph 7 from:

Exchanges the values pointed to by the two iterators a and b.

to

swap(*a, *b).

Rationale:

It's useful to say just whatiter_swap does. There may be some iterators for which we want to specializeiter_swap, but the fully general version should have a general specification.

Note that in the specific case oflist<T>::iterator,iter_swap should not be specialized as suggested above. That would domuch more than exchanging the two iterators' values: it would changepredecessor/successor relationships, possibly moving the iterator fromone list to another. That would surely be inappropriate.


[8]ページ先頭

©2009-2026 Movatter.jp