Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
328 views
in Technique[技术] by (71.8m points)

c++ - How can I improve this design that forces me to declare a member function const and declare variables mutable?

For some reason I am iterating over elements of a class in an std::set and would like to slightly modify the keys, knowing that the order will be unchanged.

Iterators on std::set are const_iterators because if the key is modified, it might result in a bad order and therefore in set corruption. However I know for sure that my operations won't change the order of my elements in the set.

For the moment, here is my solution:

class Foo
{
public:
    Foo(int a, int b): a_(a),b_(b) {}
   ~Foo(){}
    bool operator < (const Foo& o) const { return this.a_ < o.a_ ; }
    void incrementB() const { ++b_; } // <-- the problem: it is not const!
private:
    const int a_;
    mutable int b_;                   // <-- I would like to avoid this
}

void f()
{
    std::set<Foo> s;
    // loop and insert many (distinct on a_) Foo elements;
    std::for_each(s.begin(), c.end(), [](const Foo& s) { s.incrementB(); }); // Foo must be const. iterators are const_iterators
}

How would you modify it (I know I could use an std::map but I am curious whether you can suggest other options) to remove mutable and const?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can't. Set elements are required to be const for container correctness:

It forces you to realize that the key part needs to be immutable, or the data structure invariants would be broken.

struct element 
{
     std::string key_part; // const in the set

     bool operator<(const element&o) const { return key_part<o.key_part; }

  private:
     mutable int m_cached; // non-key, *NOT* used in operator<
};

If you wanted to retain the possibility to 'express' const-ness in the non-key part, split it out into pairs and store them in a map:

std::map<std::string /*key_part*/, int /*m_cached*/> mapped;

or, more flexibly:

struct element 
{
     std::string key_part; // const in the set

     bool operator<(const element&o) const { return key_part<o.key_part; }

     struct value {
         int m_cached;
         int m_moredata; //...
     } /*not in the element itself*/;
};

std::map<element, element::value> mapped;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...