Qt-interest Archive, February 2007
How may I cast a const_iterator to an iterator ?
Message 1 in thread
Hi folks,
When writing something like :
QSet<int> set;
QSet<int>::const_iterator constitr;
QSet<int>::iterator itr ;
itr = constitr;
or :
QSet<int> set;
QSet<int>::const_iterator constitr set.insert(set.end(), 1);
the compiler complains that you cannot affect a const_iterator to an
interator.
Nothing strange so far.
But how may I "force" the cast from const to non-const iterator, provided I
need a non-const iterator (for example after an insertion) ?
I've tried various semanthics, without success.
Thanks in advance,
Best,
Nicolas
--
[ signature omitted ]
Message 2 in thread
You can't.
you can transform an iterator to a const iterator, but not the opposite,
and you should not try to force the conversion.
Matthieu
Nicolas Castagne wrote:
> Hi folks,
>
>
> When writing something like :
>
> QSet<int> set;
> QSet<int>::const_iterator constitr;
> QSet<int>::iterator itr ;
> itr = constitr;
>
> or :
>
> QSet<int> set;
> QSet<int>::const_iterator constitr set.insert(set.end(), 1);
>
> the compiler complains that you cannot affect a const_iterator to an
> interator.
>
> Nothing strange so far.
>
>
> But how may I "force" the cast from const to non-const iterator, provided I
> need a non-const iterator (for example after an insertion) ?
>
> I've tried various semanthics, without success.
>
> Thanks in advance,
> Best,
> Nicolas
--
[ signature omitted ]
Message 3 in thread
"Nicolas Castagne" <castagne@xxxxxxx> wrote in message
news:es3no5$6cu$1@xxxxxxxxxxxxxxxxxxxxx
> Hi folks,
>
>
> When writing something like :
>
> QSet<int> set;
> QSet<int>::const_iterator constitr;
> QSet<int>::iterator itr ;
> itr = constitr;
>
> or :
>
> QSet<int> set;
> QSet<int>::const_iterator constitr set.insert(set.end(), 1);
>
> the compiler complains that you cannot affect a const_iterator to an
> interator.
>
> Nothing strange so far.
>
>
> But how may I "force" the cast from const to non-const iterator, provided
> I
> need a non-const iterator (for example after an insertion) ?
>
> I've tried various semanthics, without success.
Does const_cast do what you want?
--
[ signature omitted ]
Message 4 in thread
> Does const_cast do what you want?
That was my first thought, too, when I read the question. The second was
"Uh-oh, you shouldn't do that; keep your fingers off it. Who tells you
iterator works the same way const_iterator does? You're asking to
corrupt your QSet this way, or segfaulting or creating wild pointers...!"
Martin
--
[ signature omitted ]
Message 5 in thread
"Martin Gebert" <Martin.Gebert@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:es3sqd$i03$1@xxxxxxxxxxxxxxxxxxxxx
> > Does const_cast do what you want?
>
> That was my first thought, too, when I read the question. The second was
> "Uh-oh, you shouldn't do that; keep your fingers off it. Who tells you
> iterator works the same way const_iterator does? You're asking to
> corrupt your QSet this way, or segfaulting or creating wild pointers...!"
Agreed. Actually, I don't think const_cast will let you do what
he wants with an iterator. At least it doesn't seem to let
you with std::iterator.
--
[ signature omitted ]
Message 6 in thread
The reason that you get const iterators from Qset (and also STL's set<>)
is that if it were non-const it would allow you to change the key that
provides the order cause the set be out of order and all hell breaks
loose.
If you need a non-const iterator to change some member of the object
(but not the key), make the member mutable. If you need to change the
key, remove the object from the set, change the key and reinsert it.
William R Volz, Staff Research Scientist
Emerging Technology, Strategy & Consulting Team
Technical Computing Technology Department
Chevron Corporation
Energy Technology Company
#19094A
1500 Louisiana
Houston, TX, 77002
Tel 832 854 6738 Fax 832 854 6721
It is not the strongest of the species that survive, nor the most
intelligent, but the ones most responsive to change - Charles Darwin
><mailto:bill.volz@xxxxxxxxxxx>
>This message may contain confidential information that is proprietary
to ChevronTexaco and is intended only for the use of the parties to whom
it is addressed. If you are not an intended recipient, you are hereby
notified that any disclosure, copying, distribution or use of any
information in this message is strictly prohibited. If you have received
this message in error, please notify me immediately at the telephone
number noted above
-----Original Message-----
From: Nicolas Castagne [mailto:castagne@xxxxxxx]
Sent: Wednesday, February 28, 2007 4:50 AM
To: qt-interest@xxxxxxxxxxxxx
Subject: How may I cast a const_iterator to an iterator ?
Hi folks,
When writing something like :
QSet<int> set;
QSet<int>::const_iterator constitr;
QSet<int>::iterator itr ;
itr = constitr;
or :
QSet<int> set;
QSet<int>::const_iterator constitr set.insert(set.end(), 1);
the compiler complains that you cannot affect a const_iterator to an
interator.
Nothing strange so far.
But how may I "force" the cast from const to non-const iterator,
provided I need a non-const iterator (for example after an insertion) ?
I've tried various semanthics, without success.
Thanks in advance,
Best,
Nicolas
--
[ signature omitted ]
Message 7 in thread
Surprise: Reading the QSet docs (4.1.4) it seems QSet doesn't even
provide a non-const iterator. If there isn't more to it, it seems you're
supposed to use the methods QSet provides for manipulating (and the
reasonable question occurs: Why not?).
Martin
--
[ signature omitted ]
Message 8 in thread
Actually iterator and const_iterator might be an entirely different
classes (it is implementation specific), so you won't be able to cast
them unless they explicitly allow it by defining cast operator. The
following example illustrates it:
class MyContainer {
class A {
const T& operator*() const;
};
class B {
T opeator*();
};
public:
typedef A const_iterator;
typedef B iterator;
};
You can't, of cource, cast A to B of vice versa as they are unrelated
classes, untill, for example one write:
class A {
const T& operator*() const;
};
class B {
T opeator*();
operator A () const; //cast operator
};
Back to the original question, it is obvious that no conversion from
const_iterator -> iterator was defined, as it is dangerous and not
always possible - in some containers, const iterators are implemented
different from mutable to allow more efficient implementation.
On other hand, if underlying type of the iterator and const_iterator
is the same (like in vector for example) the cast would work
physically, but it is hole in a type system, not feature.
On 2/28/07, Nicolas Castagne <castagne@xxxxxxx> wrote:
> Hi folks,
>
>
> When writing something like :
>
> QSet<int> set;
> QSet<int>::const_iterator constitr;
> QSet<int>::iterator itr ;
> itr = constitr;
>
> or :
>
> QSet<int> set;
> QSet<int>::const_iterator constitr set.insert(set.end(), 1);
>
> the compiler complains that you cannot affect a const_iterator to an
> interator.
>
> Nothing strange so far.
>
>
> But how may I "force" the cast from const to non-const iterator, provided I
> need a non-const iterator (for example after an insertion) ?
>
> I've tried various semanthics, without success.
>
> Thanks in advance,
> Best,
> Nicolas
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
--
[ signature omitted ]