C++ Logo


Advanced search

[std-proposals] Alteration of elements in a set (non-const iterator)

From: Frederick Virchanza Gotham <cauldwell.thomas_at_[hidden]>
Date: Sun, 21 May 2023 22:09:30 +0100
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?

Received on 2023-05-21 21:09:41