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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:35:07+00:00 2026-05-22T19:35:07+00:00

I looked at STL’s forward iterator. I don’t see any virtual functions anywhere. If

  • 0

I looked at STL’s forward iterator. I don’t see any virtual functions anywhere. If I have a library that wants a forward iterator of strings how can I allow the function to take any forward iterator that returns strings? Is there anything in the standard library I can use?

  • 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-22T19:35:08+00:00Added an answer on May 22, 2026 at 7:35 pm

    There are no virtual methods as they are not meant to be used polymorphically (in the common sense of runtime polymorphism) but rather through templates (static polymorphism). The common approach is having the function that takes the iterators templatized on the type of the iterator. You can find many examples in the STL algorithm header:

    template <class InputIterator, class Distance>
    inline void distance(InputIterator first, InputIterator last, Distance& n);
    

    In your particular case:

    template <class ForwardIterator> // or InputIterator or whatever your needs are
    void acceptIterators( ForwardIterator start, ForwardIterator end );
    

    The additional restriction that it has to refer to strings can be implemented in term of type traits and enable_if:

    template <class ForwardIterator>
    std::enable_if< std::is_same< std::string,
                                  typename iterator_traits<ForwardIterator>::value_type > >
    void acceptIterators( ForwardIterator start, ForwardIterator end );
    

    Using typetraits from c++0x, but the enable_if and is_same are available in boost if you need them in a compiler that does not have support for them.

    If you need to be able to switch the types of iterators at runtime, you might want to look into any_iterator that performs type erasure on the specific iterators providing a runtime polymorphic interface. It is described in the article On the Tension Between Object-Oriented and Generic Programming in C++, the implementation can be found in Adobe STLab.

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

Sidebar

Related Questions

I looked at the google-chrome-extension labs page and I don't see any example where
Looked at android documentation and it appears that we don't have the ability to
I looked into the GCC STL (4.6.1) and saw that std::copy() uses an optimized
I have been developing a library that is getting pretty large, and now I
I looked through some code and noticed that the convention was to turn pointer
I looked at this example http://msdn.microsoft.com/en-us/library/bb399420.aspx and clicked on DataContext.CreateDatabase which brought me to
I looked up in the net, but cannot find any answer (maybe my search
Is it possible to forward declare a class that uses default arguments without specifying
Looked to build an add-on for IE that records the websites visited by the
Looked on google and couldn't find anything. Any good resources to get started designing

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.