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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:00:31+00:00 2026-05-24T16:00:31+00:00

I’m creating a custom iterator type, and the only use case right now is

  • 0

I’m creating a custom iterator type, and the only use case right now is std::for_each. But apparently, it’s not enough to mimic the pointer interface (I’m only doing forward iteration), there are like, a bajillion nested typedefs. I managed to figure out what to put for iterator_category, but I’m having real trouble figuring out what value_type and pointer and reference should be, because, y’know, I’m not building a container here, it’s an iterator. Why would for_each even want to know or care? All it’s going to do is forward said on to another function.

  • 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-24T16:00:33+00:00Added an answer on May 24, 2026 at 4:00 pm

    If you want to use a type T as an iterator, you must ensure that std::iterator_traits can be specialized for that type. That means you either need to provide the five nested typedefs that it defers to by default, or you need to specialize std::iterator_traits yourself. The five nested typedefs it requires are

    • difference_type, which is some type that can represent the distance between two iterators (e.g., as would be returned by std::distance)

    • value_type, which is the type of the object pointed to by the iterator

    • pointer, which is the return type of the iterator type’s operator->. This doesn’t necessarily need to be a pointer type and it doesn’t necessarily need to be value_type* or value_type const*. For example, if you have an iterator that generates elements, you may not have an object to which you can return a pointer. In that case, you might return an object that wraps the returned element and overloads operator-> itself.

    • reference, which is the return type of the iterator type’s operator*. This doesn’t necessarily need to be a reference type and it doesn’t necessarily need to be value_type& (or value_type const&). For example, If you’re iterating over an immutable range of integers, you might just return the element by value, for performance reasons.

    • iterator_category, which must be one of the iterator category tags or a type derived from one of those tags: input_iterator_tag, output_iterator_tag, forward_iterator_tag, bidirectional_iterator_tag, and random_access_iterator_tag (all in namespace std). Algorithms can use these to select an optimal algorithm based on the iterator category.

    You can’t omit any of these; they all have to be defined. That said, sometimes one or more of the typedefs may not make sense. For example, if you have an iterator that generates char elements on the fly, your iterator may not implement operator-> (because char is not a class type). In this case, you might consider just using void for the pointer type, since it should never be used anyway.

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

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I want use html5's new tag to play a wav file (currently only supported
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

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.