Coder Social home page Coder Social logo

obj.erase(it) fails on map about trial.protocol HOT 8 OPEN

breese avatar breese commented on August 29, 2024
obj.erase(it) fails on map

from trial.protocol.

Comments (8)

vinipsmaker2 avatar vinipsmaker2 commented on August 29, 2024

I forgot to fill more details. it is the value returned from obj.key_begin() and obj is dynamic::variable

from trial.protocol.

breese avatar breese commented on August 29, 2024

Good catch.

The resolution gives me some headache though. There are two solutions we can use:

  1. Add a key_iterator erase(key_iterator) member function.
  2. Add an explicit conversion from key_iterator to const_iterator, e.g. key_iterator::base() -> const_iterator.

The first solution enables the user to loop over all elements and erase some of them. That is:

for (auto it = v.key_iterator(); it != v.key_end(); /* increment done below */)
{
  if (should_we_erase_element(*it))
    it = v.erase(it); // erase returns a key_iterator
  else
    ++it;
}

The second solution has the advantage of being more general, so it can be used for other functions, like insert(). However, the example above cannot be used.

I am leaning towards the second solution because:

  • We already have the looping problem with iterator erase(const_iterator) which was introduced in C++11 (via proposal N2350.)
  • There is precedence for the explicit iterator conversion with reverse_iterator::base().

from trial.protocol.

vinipsmaker avatar vinipsmaker commented on August 29, 2024

Why can't the example compile with solution #2?

from trial.protocol.

breese avatar breese commented on August 29, 2024

Because it has type key_iterator and erase returns type iterator.

There is no conversion from iterator to key_iterator because the latter needs an extra member variable.

from trial.protocol.

breese avatar breese commented on August 29, 2024

Regarding your comment about parsing the entire input to a dynamic::variable, you will be pleased to know that I am working on a json::parse and json::format that works on dynamic::variable without the need for Boost.Serialization.

from trial.protocol.

vinipsmaker avatar vinipsmaker commented on August 29, 2024

We already have the looping problem with iterator erase(const_iterator) which was introduced in C++11 (via proposal N2350.)

Okay. I've read N2350 (except for the proposed wording chapter).

I think if we apply the same solution here, the proper solution would be to add key_erase to match the begin/key_begin split. key_erase would receive a const_key_iterator and return a key_iterator.

I wouldn't mind to have a overloaded erase function instead typing the key_ thou.

Regarding your comment about parsing the entire input to a dynamic::variable, you will be pleased to know that I am working on a json::parse and json::format that works on dynamic::variable without the need for Boost.Serialization.

Surely I'll enjoy that.

from trial.protocol.

breese avatar breese commented on August 29, 2024

I have added a key_iterator::base() function to convert a key_iterator into a const_iterator in db775c4, so now v.erase(kit.base()) is doable.

Regarding the key_erase() function, I would rather have such functionality as algorithms, e.g. like the dynamic::key::count() and dynamic::key::find() algorithms, to prevent a proliferation of the variable interface.

from trial.protocol.

vinipsmaker avatar vinipsmaker commented on August 29, 2024

Okay. Let's leave the issue open for some time in case others want to pop in and add their comments.

I have added a key_iterator::base() function to convert a key_iterator into a const_iterator in db775c4, so now v.erase(kit.base()) is doable.

This will suit me for now. Probably I'll send a PR in the following weeks with one dynamic::key::erase. The question of a sweet spot here between convenience and a clean interface is a tricky one. Not many ready guidelines we can adhere to. But also, it's the problems we always face with novel design (otherwise they wouldn't be novel).

from trial.protocol.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.