Date: Wed, 24 May 2023 08:45:37 +0200
On Sun, May 21, 2023 at 10:09:30PM +0100, Frederick Virchanza Gotham via Std-Proposals wrote:
> I wrote the following code today; I have a struct for storing an IP
> address as well as two booleans (one for a FTP server and one for a
> Samba file server):
>
> struct IPFileShare {
> std::uint32_t ip;
> bool ftp, smb;
>
> IPFileShare(uintIP const arg) : ip(arg), ftp(false), smb(false) {}
>
> bool operator<(IPFileShare const &rhs) const { return this->ip < rhs.ip; }
> };
>
> std::set<IPFileShare> myset;
>
> As you can see, the sorting operator only takes into account the IP
> address. The other two members of the struct, 'ftp' and 'smb', have no
> effect on the sorting of the items in the set.
>
> After having added an item to the set, I want to change the values of
> the two booleans, which should be fine because these booleans have no
> effect on the sorting. I wrote the following code to achieve this:
>
> const_cast<bool&>(myset.insert(ip.num).first->ftp) = true;
>
> I think strictly speaking, the Standard says that this is undefined
> behaviour. How about we change the Standard to say that this
> const_cast is allowed so long as it has no effect on the sorting?
Here it looks like you are using the wrong container, you really want
tree fromn stlport which sadly was lost during the standardization.
The signature of tree is usually something like
tree<ValueType, KeyType, KeyFromValue, KeyComp, Allocator>
and the requirement on changes is that KeyFromValue(value) never changes
so this fits your use case like a glove.
What is even better is that this class already is present in your standard
library as that usually implements map and set as some variation of
using set<T, Comp, Allocator> = tree<T, T, _identity, Comp, Allocator>;
and
using map<K, V, KeyComp, Allocator> =
tree<std::pair<const K, V>, K, _getfirst, KeyComp, Allocator>;
Yes, tree is the more useful container, map and set are only two
specializations of it.
/MF
> I wrote the following code today; I have a struct for storing an IP
> address as well as two booleans (one for a FTP server and one for a
> Samba file server):
>
> struct IPFileShare {
> std::uint32_t ip;
> bool ftp, smb;
>
> IPFileShare(uintIP const arg) : ip(arg), ftp(false), smb(false) {}
>
> bool operator<(IPFileShare const &rhs) const { return this->ip < rhs.ip; }
> };
>
> std::set<IPFileShare> myset;
>
> As you can see, the sorting operator only takes into account the IP
> address. The other two members of the struct, 'ftp' and 'smb', have no
> effect on the sorting of the items in the set.
>
> After having added an item to the set, I want to change the values of
> the two booleans, which should be fine because these booleans have no
> effect on the sorting. I wrote the following code to achieve this:
>
> const_cast<bool&>(myset.insert(ip.num).first->ftp) = true;
>
> I think strictly speaking, the Standard says that this is undefined
> behaviour. How about we change the Standard to say that this
> const_cast is allowed so long as it has no effect on the sorting?
Here it looks like you are using the wrong container, you really want
tree fromn stlport which sadly was lost during the standardization.
The signature of tree is usually something like
tree<ValueType, KeyType, KeyFromValue, KeyComp, Allocator>
and the requirement on changes is that KeyFromValue(value) never changes
so this fits your use case like a glove.
What is even better is that this class already is present in your standard
library as that usually implements map and set as some variation of
using set<T, Comp, Allocator> = tree<T, T, _identity, Comp, Allocator>;
and
using map<K, V, KeyComp, Allocator> =
tree<std::pair<const K, V>, K, _getfirst, KeyComp, Allocator>;
Yes, tree is the more useful container, map and set are only two
specializations of it.
/MF
Received on 2023-05-24 06:45:40