Board index » cppbuilder » converting to cb2007 from b5: iterators are const?

converting to cb2007 from b5: iterators are const?


2007-12-03 07:08:02 PM
cppbuilder30
It seems iterator are const in 2007 and not only const_iterator
This worked in builder 5
#include <vcl.h>
#pragma hdrstop
#include <set>
using namespace std;
struct MyClass
{
int id;
int data;
MyClass() : id(0), data(0) {}
MyClass(int _id) : id(_id), data(0) {}
friend bool operator<(const MyClass &lhs, const MyClass &rhs)
{ return (lhs.id < rhs.id); }
friend bool operator!=(const MyClass &lhs, const MyClass &rhs)
{ return (lhs.id != rhs.id); }
};
#pragma argsused
int main(int argc, char* argv[])
{
set<MyClass>theData;
MyClass tmp(1);
theData.insert(tmp);
set<MyClass>::iterator it = theData.find(tmp);
if (it != theData.end())
{
it->data = 2; // [BCC32 Error] File2.cpp(29): E2024 Cannot modify a const object
MyClass *p = &(*it); // [BCC32 Error] File2.cpp(31): E2034 Cannot convert 'const MyClass *' to 'MyClass *'
}
return 0;
}
//---------------------------------------------------------------------------
 
 

Re:converting to cb2007 from b5: iterators are const?

"Peter Strömberg" < XXXX@XXXXX.COM >writes:
Quote
It seems iterator are const in 2007 and not only const_iterator

This worked in builder 5
For std::set, yes, but that's what the standard requires. That it
"worked" in bcb5 must have been a library bug upon which you
inadvertantly created a dependency.
The reason a std::set iterator behaves like a const_iterator is the
same reason that the "key" in a std::map is const: If you change the
data in the set in a way that breaks the ordering, you cause undefined
behavior.
This was one area where the committee wanted to err on the side of
caution, since if you're sure it's safe you can cast away the
constness of the data (using const_cast<>) to which the iterator
refers, provided you're sure that you're not touching data that forms
part of the sort criteria.
They left an option for you to "go manual" here, but now the checking
is clearly left as a responsibility to you, once you cast away
constness.
--
Chris (TeamB);
 

Re:converting to cb2007 from b5: iterators are const?

"Peter Strömberg" < XXXX@XXXXX.COM >wrote in message
Quote
It seems iterator are const in 2007 and not only const_iterator

This worked in builder 5
The STL that is provide with BCB has changed over the years. BCB 5 shipped
with RogueWave, BCB 6 with STLPort and RogueWave (STLPort was the default
unless a compiler override was defined), and RAD2007 with Dinkumware now.
Gambit
 

{smallsort}