Accessing member via set's NON-const iterator that doesn't affect invariants

A

Anand Hariharan

Let's say I have code like so:

#include <set>
#include <math.h>

typedef int OtherTypes;

struct MyType
{
double Field1;
OtherTypes MoreFields;

MyType(double blah) :
Field1(blah)
{
}

bool operator < (const MyType &That) const
{
// Does not use any other member
return ( fabs(Field1 - That.Field1) > 1e-6 &&
Field1 < That.Field1 );
}
};

int main()
{
std::set<MyType> foo;
std::pair< std::set<MyType>::iterator,
bool > inchk = foo.insert(MyType(1.0));

OtherTypes SomeVal = 1;
if ( inchk.second )
inchk.first->MoreFields = SomeVal; // error
}


How do I reassure the compiler that writing MoreFields will not affect
any invariants or will not do anything to invalidate the order of
elements in the set?

If the only recourse is to use another container such as vector, how
do I insert a new value in the sorted position while checking if one
exists already?
 
V

Victor Bazarov

Let's say I have code like so:

#include <set>
#include <math.h>

typedef int OtherTypes;

struct MyType
{
double Field1;
OtherTypes MoreFields;

MyType(double blah) :
Field1(blah)
{
}

bool operator < (const MyType &That) const
{
// Does not use any other member
return ( fabs(Field1 - That.Field1) > 1e-6 &&
Field1 < That.Field1 );
}
};

int main()
{
std::set<MyType> foo;
std::pair< std::set<MyType>::iterator,
bool > inchk = foo.insert(MyType(1.0));

OtherTypes SomeVal = 1;
if ( inchk.second )
inchk.first->MoreFields = SomeVal; // error
}


How do I reassure the compiler that writing MoreFields will not affect
any invariants or will not do anything to invalidate the order of
elements in the set?

Perhaps you should declare 'MoreFields' "mutable"?
If the only recourse is to use another container such as vector, how
do I insert a new value in the sorted position while checking if one
exists already?

If your vector is sorted, use 'lower_bound' (or is it 'upper_bound'?) to
find the place where the sorting would place your element, then compare
what you want to insert to what's already there (the next element or the
previous, can't divine now) thus /checking/ if one exists.

V
 
A

Anand Hariharan

Perhaps you should declare 'MoreFields' "mutable"?

Thank you, Victor! I'd forgotten all about that keyword despite that
I'd argued a case for that keyword with a colleague many years ago.

If your vector is sorted, use 'lower_bound' (or is it 'upper_bound'?) to
find the place where the sorting would place your element, then compare
what you want to insert to what's already there (the next element or the
previous, can't divine now) thus /checking/ if one exists.

That is more tedious than the alternative of simply declaring other
fields as mutable, IMHO.

thank you again,
- Anand
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top