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

  • SEARCH
  • Home
  • 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 6618009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T20:46:25+00:00 2026-05-25T20:46:25+00:00

I’m trying to use boost iterator facade to implement an iterator for a class

  • 0

I’m trying to use boost iterator facade to implement an iterator for a class that stores a sorted vector of elements of type data_t. Currently I’m having troubles with dereferencing it. I only need the iterator for traversal and searching, the iterator doesn’t need to change any of the internal state of the Range object.

Here’s the range.hpp:

#include <algorithm>
#include <vector>
#include <sstream>
#include <stdexcept>
#include <functional>

#include <boost/iterator/iterator_facade.hpp>

struct testRangeImpl{
    typedef unsigned int data_t;

    struct RangeOrdering : public std::binary_function< data_t const &, data_t const &, bool >{
        bool operator()(data_t const& a, data_t const& b){
            return a < b;
        }
    };
};


template<
    typename ImplT 
>
class SortedRange: public boost::iterator_facade< 
                    SortedRange< ImplT >,  //this type because of the CRTP
                    typename ImplT::data_t, //The type of the data
                    boost::bidirectional_traversal_tag //iterators can be incremented and decremented
                >{
    public:
        /*! this type */
        typedef SortedRange< ImplT > type;

        /*! The type of the implementation policy */
        typedef ImplT impl_t; 

        /*! The internal representation of an element */
        typedef typename impl_t::data_t data_t;

        /*! The internal representation of a range */
        typedef std::vector< data_t > range_t;

        /*! A member variabe to keep track of if the range has been sorted */
        bool m_sorted;

        /*! The actual range itself in its internal representation */
        range_t m_range;

        /*! The actual range itself in its internal representation */
        size_t m_range_size;

        /*! An exception indicating an invalid range */
        struct InvalidRangeException{};

        /*! Current element for iterator */
        size_t m_current_combo;

        enum class PositionClass {
            NOT_END,
            END,
            REND
        };

        explicit SortedRange( )
            : m_sorted(false), m_range(), m_range_size(0), m_current_combo(0) , m_posclass(PositionClass::END){
        }

        explicit SortedRange(std::vector < data_t > const& rg, size_t const& current_combo, PositionClass const& p )
            : m_sorted(false), m_range(rg), m_range_size(rg.size()), m_current_combo(current_combo) , m_posclass(p){
            if(rg.empty()){
                throw InvalidRangeException();
            }
            std::sort(m_range.begin(),m_range.end(),typename ImplT::RangeOrdering());
            m_sorted = true;
            //initialise();
        }

    protected:
        explicit SortedRange(std::vector < data_t > const& rg)
            : m_sorted(false), m_range(rg), m_range_size(rg.size()), m_current_combo(0) , m_posclass(PositionClass::NOT_END){
            if(rg.empty()){
                throw InvalidRangeException();
            }
            std::sort(m_range.begin(),m_range.end(),typename ImplT::RangeOrdering());
            m_sorted = true;
            //initialise();
        }   

        /*! Implementation policy object */
        impl_t m_impl;

        /*! construct a range with a specific internal state */
        explicit SortedRange(std::vector < data_t > const& rg, size_t const& current_combo)
            : m_sorted(false), m_range(rg), m_range_size(rg.size()), m_current_combo(current_combo) , m_posclass(PositionClass::NOT_END){
            if(rg.empty()){
                throw InvalidRangeException();
            }
            std::sort(m_range.begin(),m_range.end(),typename ImplT::RangeOrdering());
            m_sorted = true;
            //initialise();
        }
    public:

        size_t size(){
            m_range_size = m_range.size();
            return m_range_size;
        }

        /* Return first data */
        type begin() const {
            return type(m_range, 0);
        }

        type end() const {
            return type(m_range, m_current_combo, PositionClass::END);
        }

        type rend() const {
            return type(m_range, m_current_combo, PositionClass::REND);
        }

        /* Return last data */
        type rbegin() const {
            return type(m_range, m_range_size -1 );
        }

    private:
        friend class boost::iterator_core_access;

        /*! Position class */
        PositionClass m_posclass;

        /*! set up the initial state */
        void initialise() {
            std::sort(m_range.begin(),m_range.end());
            m_sorted == true;
        }

        /*! the first element */
        data_t first() const {
            return m_range[0];
        }

        /*! the last element */
        data_t last() const {
            return m_range[m_range_size - 1];
        }


        /*! return the current element */
        const data_t& dereference() const  {
            if(m_posclass == PositionClass::NOT_END) {
                return m_range[m_current_combo];
            }else {
                throw std::out_of_range("Attempt to dereference past the valid range");
            }
        }

        /*! get the next combination */
        void increment() {
            if(m_posclass != PositionClass::NOT_END)
                throw std::out_of_range("Cannot increment past the valid range");

            if(m_current_combo == m_range_size ) {
                //current combination is the last
                m_posclass = PositionClass::END;
            }
            m_current_combo++;
        }

        /*! get the previous combination */
        void decrement() {
            if(m_posclass != PositionClass::NOT_END)
                throw std::out_of_range("Cannot decrement past the valid range");

            if(m_current_combo == 0) {
                //current combination is the first
                m_posclass = PositionClass::REND;
            }
            m_current_combo--;  
        }

        /*! check for equality between two iterators. */
        bool equal(type const& other) const {
           if(m_posclass == PositionClass::NOT_END && 
           other.m_posclass == PositionClass::NOT_END) {

               return 
                   m_current_combo == other.m_current_combo &&
                   m_range == other.m_range;
           }
           else {
                return m_posclass == other.m_posclass && m_range == other.m_range;
           }
        }
};

struct Range : public SortedRange< testRangeImpl>{
    /*! An exception we throw if someone tries to construct an invalid range */
    struct InvalidRangeException : public SortedRange< testRangeImpl >::InvalidRangeException {};

    /*! Construct a range from a vector of unsigned ints */
    explicit Range(std::vector<unsigned int> const& r) : SortedRange<testRangeImpl>(r)
    {
        if(r.empty()){
            throw InvalidRangeException();
        }
    }
};

Here’s the main.cpp:

#include <vector>
#include <iostream>
#include "range.hpp"
int main(void){
    std::vector<unsigned int> rg  = { 2 , 5 , 1 , 3 , 4 };
    Range test(rg);
    for(auto elem: test){
        std::cout << elem << " " ;
    }
    return 0;
}

Compiling gives this error:

/usr/include/boost/iterator/iterator_facade.hpp: In static member function ‘static typename Facade::reference boost::iterator_core_access::dereference(const Facade&) [with Facade = SortedRange<testRangeImpl>, typename Facade::reference = unsigned int&]’:
/usr/include/boost/iterator/iterator_facade.hpp:643:67:   instantiated from ‘boost::iterator_facade<I, V, TC, R, D>::reference boost::iterator_facade<I, V, TC, R, D>::operator*() const [with Derived = SortedRange<testRangeImpl>, Value = unsigned int, CategoryOrTraversal = boost::bidirectional_traversal_tag, Reference = unsigned int&, Difference = long int, boost::iterator_facade<I, V, TC, R, D>::reference = unsigned int&]’
main.cpp:9:17:   instantiated from here
/usr/include/boost/iterator/iterator_facade.hpp:517:32: error: invalid initialisation of reference of type ‘boost::iterator_facade<SortedRange<testRangeImpl>, unsigned int, boost::bidirectional_traversal_tag, unsigned int&, long int>::reference {aka unsigned int&}’ from expression of type ‘const data_t {aka const unsigned int}’

How do I get this to be a usable iterator type?

  • 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-25T20:46:26+00:00Added an answer on May 25, 2026 at 8:46 pm

    You are trying to take a non-const reference to a const value. For some reason you have decided that dereference returns a const reference, instead of honoring the facade reference type. Fix that, and it should work.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
I am trying to loop through a bunch of documents I have to put
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

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.