473,805 Members | 2,172 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

map, iterators and swap

Hi All,

typedef std::list<int> Cont;
void f(Cont &c1, Cont &c2)
{
Cont::iterator it = c1.begin();
c1.swap(c2);
it == c2.begin(); // is this ill formed?
}

Visual C++ 2005 has fancy iterator debugging, and it asserts at the
last line saying that iterators are incompatible and cannot be
compared. Basically, when in debugging mode it stores pointer to
container with each iterator, and this pointer obviously still points
to c1 despite the swap, so comparison with iterator obtained from
c2.begin() fails.

Could somebody enlighten me if this is correct behaviour (i.e. the code
is ill formed), or if this is VC 2005 bug.

cheers,
Marcin

Oct 25 '05 #1
4 2693
On 2005-10-25, ka****@poczta.o net.pl <ka****@poczta. onet.pl> wrote:
Hi All,

typedef std::list<int> Cont;
void f(Cont &c1, Cont &c2)
{
Cont::iterator it = c1.begin();
c1.swap(c2);
it == c2.begin(); // is this ill formed?
}

Could somebody enlighten me if this is correct behaviour (i.e.
the code is ill formed), or if this is VC 2005 bug.


According to Dinkumware's standard libary documentation it is
well formed when c1.allocator == c2.allocator.

I wouldn't call it a VC bug. In most cases, comparing iterators
from two different containers would be an error.

--
Neil Cerutti
Oct 25 '05 #2

ka****@poczta.o net.pl wrote:
Hi All,

typedef std::list<int> Cont;
void f(Cont &c1, Cont &c2)
{
Cont::iterator it = c1.begin();
c1.swap(c2);
it == c2.begin(); // is this ill formed?
}

Visual C++ 2005 has fancy iterator debugging, and it asserts at the
last line saying that iterators are incompatible and cannot be
compared. Basically, when in debugging mode it stores pointer to
container with each iterator, and this pointer obviously still points
to c1 despite the swap, so comparison with iterator obtained from
c2.begin() fails.

Could somebody enlighten me if this is correct behaviour (i.e. the code
is ill formed), or if this is VC 2005 bug.


Comparing iterators from different containers is an open C++ library
issue (#446) but the intention is apparently that such comparisons are
undefined.

However, the comparison in the program above should be OK (at least in
practice) since the containers have been swapped and the iterators
being compared are std::list iterators which are not easily prone to
invalidation.

Greg

Oct 25 '05 #3
> ka****@poczta.o net.pl wrote:
Hi All,

typedef std::list<int> Cont;
void f(Cont &c1, Cont &c2)
{
Cont::iterator it = c1.begin();
c1.swap(c2);
it == c2.begin(); // is this ill formed?
}
Comparing iterators from different containers is an open C++ library
issue (#446) but the intention is apparently that such comparisons are
undefined.

However, the comparison in the program above should be OK (at least in
practice) since the containers have been swapped and the iterators
being compared are std::list iterators which are not easily prone to
invalidation.


Yes in practice it works everywhere I tested except in VS2005 when
iterator debugging is on. But my question is if calling swap does make
the above comparison legal. In other words what are the sematics of
iterator after swap? Should I treat it as iterator to the old container
(even though it now points to element in new container), and apply rule
about comparing iterators from different containers? Sigh...
From what I know, the standard says only std::basic_stri ng iterators

are invalidated by swap. Iterators from the rest of the containers
should be untouched. But what about my comparison failing then?

cheers,
Marcin

Oct 25 '05 #4
In article <11************ **********@g14g 2000cwa.googleg roups.com>,
ka****@poczta.o net.pl wrote:
Hi All,

typedef std::list<int> Cont;
void f(Cont &c1, Cont &c2)
{
Cont::iterator it = c1.begin();
c1.swap(c2);
it == c2.begin(); // is this ill formed?
}

Visual C++ 2005 has fancy iterator debugging, and it asserts at the
last line saying that iterators are incompatible and cannot be
compared. Basically, when in debugging mode it stores pointer to
container with each iterator, and this pointer obviously still points
to c1 despite the swap, so comparison with iterator obtained from
c2.begin() fails.

Could somebody enlighten me if this is correct behaviour (i.e. the code
is ill formed), or if this is VC 2005 bug.
This is simply a bug in the debugging STL. A container swap should also
swap the iterator owners (in debug mode). No, you won't find that in
the standard, the standard doesn't address debug mode. But the standard
does say:
no swap() function invalidates any references, pointers, or iterators
referring to the elements of the containers being swapped.


In the implementation I'm running on, in debug mode, the ownership test
is executed and passes.

-Howard
Oct 25 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2308
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much of a performance hit I'm taking using this technique versus using 'copy' to copy directly into a container with elements in place? Thanks, d
24
3969
by: Lasse Vågsæther Karlsen | last post by:
I need to merge several sources of values into one stream of values. All of the sources are sorted already and I need to retrieve the values from them all in sorted order. In other words: s1 = s2 = s3 = for value in ???(s1, s2, s3):
2
2357
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
4
4102
by: Niels Dekker (no reply address) | last post by:
When calling swap as follows (as recommanded in Effective C++, 3rd Edition, by Scott Meyers), what swap is chosen to be called? using std::swap; swap(a, b); Suppose there is a global ::swap function provided, whose parameter type matches closer to the type of a and b than any of the std::swap overloads does. Will this ::swap be called, or is std::swap still preferred? I ask this because the compilers I tried disagree! So will any of...
9
6202
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
8
1594
by: pvonnied | last post by:
Hi, Once more a question (sorry for the different e-mail addresses , here @ work we have to use Google to post to newsgroups): If I use an iterator, say from std::map, I initialize it like so: <--- ---> std::map<int, myClass*myCollection; std::map<int, myClass*>::iterator it = myCollection.begin(); for(; it != myCollection.begin(); ++it) { // do something }
18
2125
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
11
4174
by: Juha Nieminen | last post by:
Assume we have this: std::list<Typelist1(10, 1), list2(20, 2); std::list<Type>::iterator iter = list1.end(); list1.swap(list2); What happens here, according to the standard? 1) 'iter' still points to list1::end(). 2) 'iter' now points to list2::end().
2
4972
by: abek42 | last post by:
Hi All, I've been flummoxed by this bizarre behaviour and would like insight on how to get around it. --Snip-- struct polygon { int i; int j; }
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5544
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3846
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.