Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6255589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T14:23:34+00:00 2026-05-24T14:23:34+00:00

Is there a way to use boost foreach without defining a const_iterator ? My

  • 0

Is there a way to use boost foreach without defining a const_iterator?

My use-case for this is an iterator for a vector, that can contain invalid elements. The iterator should traverse the vector, and yield only the valid elements. It should also repair the vector in the sense that it should swap each invalid item with the next valid item and resize the vector at the end. For example if -1 represents invalid values the vector [6,-1,-1,9,-1,2] should iterate over 6,9 and 2 and leave the vector as [6,9,2].

I tried implementing this with boost::iterator_facade but I could not think of a way to implement a const_iterator, because the vector can change by removing invalid values and thus cannot be const.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T14:23:34+00:00Added an answer on May 24, 2026 at 2:23 pm

    Separation of concerns: the container is responsible for its invariants, the iterators for traversal. If you move the repairing to the container you can separate the logical const from the mutable, hidden parts.

    Can you write your iterators the ‘dumbest’ way possible to disentangle them from the container? For instance storing a numerical index (if it makes sense for your container), then calling a private friend (or more) of the container to access the logical n-th element.

    The private friend(s) can then be overloaded on const and may still modify the mutable parts to do the repairing you describe, and then return the element.


    An (abridged) example of container supporting random-access (and thus numerical indices for access):

    template<typename T>
    class vector {
        mutable std::vector<std::weak_ptr<T>> data; // notice mutable
    
        T&
        fetch(int n);
    
        T const&
        fetch(int n) const; // notice const overload
    
    public:
        class const_iterator;
        friend class const_iterator;
    
        const_iterator
        begin() const;
    };
    
    template<typename T>
    class vector<T>::const_iterator {
        int index;
        vector<T> const* this_; // notice const
    public:
        // constructors go here
    
        const_iterator&
        operator++()
        { ++index; }
        // ...
    
        T const&
        operator*() const
        { return this_->fetch(index); } // this will call the const version of fetch
    };
    
    // example implementation of the const version of fetch
    template<typename T>
    T const&
    vector<T>::fetch(int n) const
    {
        auto removed = std::remove_if(data.begin(), data.end(), [](std::weak_ptr<T>& element)
        { return element.expired(); });
        // mutate mutable data member in a logically const member
        data.erase(data.begin(), removed);
    
        // this assumes that there is no race condition
        // bear with me for the sake of understanding the mutable keyword
        return *data[n].lock();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to use the += operator with a vector without using
is there a way to use boost's threading capabilities without the entire boost library?
Boost::Serialization has builtin support for boost::shared_ptr<> . Is there a way to use this
Is there a way to use boost::filesystem::path with unicode file paths? In particular I'd
I was wondering if there was a way to use the boost library in
Is there a way to get boost.format to use and return wide (Unicode) character
Is there a way to use form fields that does not correspond to database
Is there a non-hacky (i.e. no assembly, ...) way to use boost functions to
Is there any way to do it nicely. When I try to use Boost's
Is there any way I can have thread-safe coroutines along with boost::asio? I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.