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 958281
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:50:21+00:00 2026-05-16T00:50:21+00:00

I’m defining an iterator type that does not store its current value explicitly. Instead,

  • 0

I’m defining an iterator type that does not store its current value explicitly. Instead, it’s a wrapper around another iterator, and returns std::pairs of the underlying iterator’s values and another value. The relevant part of the class definition:

class MyIterator : public std::iterator<input_iterator, std::pair<Foo *, Bar *> > {

    FooPtrIterator d_iter;
    Bar d_bar;

    public:

        MyIterator(FooPtrIterator iter, Bar const &bar)
        :
            d_iter(iter),
            d_bar(bar)
        { }

        // Returning by value, not storing the pair itself.
        value_type operator*() const {
            return std::make_pair(*d_iter, &d_bar);
        }

        // Can't return by value!
        ??? operator->() const {
            return ???;
        }

};

Now this gets me in trouble when overloading operator->, because I’m expected to return a pointer, or something else that itself supports operator->.

I could simply store the current std::pair as a class member:

    value_type d_current;

However, that gets me in trouble in the constructor, because I cannot know at that point whether FooPtrIterator iter is a valid iterator, so I cannot dereference it to provide d_current with a value.

I can also make operator-> return a boost::shared_ptr<value_type> but that is a terrible hack with more overhead than should be needed.

I get the feeling that this “generating” iterator is not supposed to exist. Is that so, or is there another way around this?

  • 1 1 Answer
  • 1 View
  • 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-16T00:50:22+00:00Added an answer on May 16, 2026 at 12:50 am

    I would suggest creating an own class holding the Foo* and Bar* pair, like this:

    class MyPair  {
    private:
      FooPtrIterator foo;
      Bar bar;
    
    public:
      MyPair(const FooPtrIterator& it, const Bar& b) : foo(it), bar(b)  {}
    
      Foo* first() { return *foo; }
      Bar* second() { return &bar; }
    };
    

    Then just use it inside your iterator class instead of the two values. You will have to change ->first and ->second to ->first() and ->second() in your code but that should be an acceptable change.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
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 have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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 am doing a simple coin flipping experiment for class that involves flipping a
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post

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.