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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:15:36+00:00 2026-05-14T21:15:36+00:00

A typical forward iterator is expected to implement following methods: value_type& operator*(); value_type* operator->();

  • 0

A typical forward iterator is expected to implement following methods:

value_type& operator*();
value_type* operator->();

I’m writing a custom iterator for a custom container where user expects to see a value_type different from representation of the value inside a container. So when returning a value_type value to user I convert it from inner representation to user-expected value_type. It is easy to implement inside mentioned members.

Could you advice how to handle l-values? Where can I handle conversion of value_type to inner representation when user assigns values to iterator in syntax like *it = value_type(5)?

I thought of returning a functor but I dislike this idea becaues of non-usual syntax on caller’s side.

  • 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-14T21:15:36+00:00Added an answer on May 14, 2026 at 9:15 pm

    You might want to take a look into the implementation of the std::vector<bool> specialization iterators, as they tackle the same problem. Note that with time, the specialization has been frown upon mainly because it does not comply with the container requirements (the iterators don’t provide references to the actual contained type, but a proxy value), and that will also be a problem with your implementation.

    The other possible approach is using a regular container but having the stored type accept implement the assignment/conversions to and from the user expected types. If that is not possible with your current stored type, you can write a wrapper for it.

    A simplified wrapper (you will need to work on it to make it work):

    template <typename T, typename U>
    class wrapper
    {
    public:
       typedef T store_type;
       typedef U value_type;
       wrapper() : stored() {}
       wrapper( value_type const & v ) 
          : stored( convert<store_type>(v) {}
       wrapper& operator=( value_type const & value ) { // or pass-by-value
          stored = convert<store_type>(value); // or however you can convert them
          return *this;
       }
       operator value_type() const { // I don't quite like this, if possible use explicit conversions
          return convert<value_type>(stored);
       }
    private:
       store_type stored; // maybe storage is handled externally and this can be pointer/ref.
    };
    // For the simple test double<->int conversion static cast suffices
    template <typename T, typename U>
    T convert( U in ) {
       return static_cast<T>(in); 
    }
    int main() {
       std::vector< wrapper<double,int> > v;
       v.push_back( 10 );
       int x = v[0];
       v[0] = 5;
    
       std::vector< wrapper<int,double> > v2;
       v.push_back( 10.5 );
       double y = v2[0];
       v2[0] = 11.3;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 433k
  • Answers 433k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer A simple example, drop a CheckBox on the form: public… May 15, 2026 at 3:00 pm
  • Editorial Team
    Editorial Team added an answer IIUC you want the empty set []: Something([ssShift]); // X… May 15, 2026 at 3:00 pm
  • Editorial Team
    Editorial Team added an answer You can see the information regarding the transfer by doing:… May 15, 2026 at 3:00 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.