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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:42:40+00:00 2026-06-02T17:42:40+00:00

I am writing a stl-like container class which has the following functions: Iterator begin(){

  • 0

I am writing a stl-like container class which has the following functions:

    Iterator begin(){
        return Iterator(data_.begin(), 1);
    }

    ConstIterator begin() const{
        return ConstIterator(data_.begin(), 1);
    }

I think I could make one function to replace both:

    template <typename itr0, typename itr1>
    itr0 begin(){
        return itr1(data_.begin(), 1);
    }

and when I call the following, the code is generated in compile time:

    Iterator it = foo.begin<Iterator, Iterator>();
    ConstIterator it = foo.begin<ConstIterator const?, ConstIterator>();

My first question is, what typename is actually ConstIterator begin() const?

Secondly, is there a way to make it so that this metaprogramming is transparent from outside of the class? i.e. I could still use the following code to call begin() as if it was written in a standard way?

    C foo;
    const C foo2;
    Iterator it = foo.begin();
    ConstIterator it = foo2.begin();
  • 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-06-02T17:42:42+00:00Added an answer on June 2, 2026 at 5:42 pm

    Unfortunately you’ll need to define the two methods separately since, as you’ve noted, their signature differs by the const modifier. There’s no template wizardry available that can overcome that (at least none that I’m aware of).

    You can however use a number of different techniques to combine their implementations into a single method. Here’s one such option that avoids the need for any const_cast‘ing:

    struct Container
    {
        template< typename I, typename C >
        friend I begin_impl( C & c ){
          return I( c.data_.begin(), 1 );
        }
    
        Iterator begin(){
            return begin_impl< Iterator >( *this ); // *this is "Container"
        }
    
        ConstIterator begin() const{
            return begin_impl< ConstIterator >( *this ); // *this is "Container const"
        }
    };
    

    See here for more options.

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

Sidebar

Related Questions

I am writing an iterator for a container which is being used in place
Need advice here: which of the STL container's operations are considered read-only? Take vector<int>
Here's a fairly normal encapsulation of an STL container which allows the user of
I am writing (as a self-teaching exercise) a simple STL-Like range. It is an
I'm writing my own implementation of the C++ STL map container. Now I'm trying
Has anyone seen an allocator that calls mlock(2) to prevent an STL container's contents
I'm writing a program which is more or less like this : #include <list>
I was trying to learn writing stl like iterators, for that I wrote a
When writing apps with asp.net, which I do more of than vb.net apps, I
For efficiency reasons, I always avoid writing loops like this: for(std::size_t i = 0;

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.