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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:27:32+00:00 2026-06-14T06:27:32+00:00

I need a function template that accepts two iterators that could be pointers. If

  • 0

I need a function template that accepts two iterators that could be pointers. If the two arguments are random_access iterators I want the return type to be an object of

std::iterator<random_access_iterator_tag, ...> type

else a

std::iterator<bidirectional_iterator_tag, ...> type.

I also want the code to refuse
compilation if the arguments are neither a bidirectional iterator, nor a pointer. I cannot have dependency on third party libraries e.g. Boost

Could you help me with the signature of this function so that it accepts bidirectional iterators as well as pointers, but not say input_iterator, output_iterator, forward_iterators.

One partial solution I can think of is the following

template<class T>
T foo( T iter1, T iter2) {
  const T tmp1 = reverse_iterator<T>(iter1);
  const T tmp2 = reverse_iterator<T>(iter2);
  // do something
}

The idea is that if it is not bidirectional the compiler will not let me construct a reverse_iterator from it.

  • 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-14T06:27:33+00:00Added an answer on June 14, 2026 at 6:27 am

    a bit simpler than previous answer, no dependency on std::enable_if:

    namespace detail
    {
        template<class T>
        T do_foo(T iter1, T iter2, std::random_access_iterator_tag t)
        {
            cout << "do_foo random_access" << endl;
            return iter1;
        }
        template<class T>
        T do_foo(T iter1, T iter2, std::bidirectional_iterator_tag t)
        {
            cout << "do_foo bidirectional" << endl;
            return iter1;
        }
    
    }
    template<class T>
    void foo(T iter1, T iter2)
    {
        typename std::iterator_traits<T>::iterator_category t;
        detail::do_foo(iter1, iter2, t);
    }
    
    int main (int argc, const char * argv[])
    {
        std::vector<int> v;
        foo(v.begin(), v.end());
        std::list<int> l;
        foo(l.begin(), l.end());
        return 0;
    }
    

    The solution also supports other iterator_categories derived from std::random_access_iterator_tag or std::bidirectional_iterator_tag (should there be any), while std::same<> checks for strict category equality.

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

Sidebar

Related Questions

I need a function that could generate a regex. For example if I write
I need a function like var_dump($object) in php The problem is that the Var_dump
Consider: template < typename Something > boost::function<void()> f() { typedef typename Something::what type; return
I want to implement a factory function for creating objects. My object template looks
I need to make a template function that receives as parameter a std::container of
I have a class with an Attach function that accepts a function object and
I need a template expression that selects the first argument type if the first
I need to create a function that will merge an array into a template
I need a function that can intersect 2 arrays for example: $Array1 = array(1,2,3);
I need a function that can print Turkish characters. public String convert(String input) {

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.