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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:35:09+00:00 2026-06-08T06:35:09+00:00

I wrote a short utility function an object to wrap an iterable container, so

  • 0

I wrote a short utility function an object to “wrap” an iterable container, so that I could walk it backwards using a range based for.

template <typename Iterable>
struct ReverseWrapper {
private:
  Iterable& m_iterable;

public:
  ReverseWrapper(Iterable& iterable) : m_iterable(iterable) {}

  auto begin() const ->decltype(m_iterable.rbegin()) {
    return m_iterable.rbegin();
  }

  auto end() const ->decltype(m_iterable.rend()) {
    return m_iterable.rend();
  }
};

template <typename Iterable>
ReverseWrapper<Iterable> reverseIterate(Iterable& list) {
  return ReverseWrapper<Iterable>(list);
}

This works for C++ iterable objects, but not for static arrays. What is required for an object to support iteration using a range based for? What would be the best way to approach this problem?

  • 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-08T06:35:10+00:00Added an answer on June 8, 2026 at 6:35 am

    The actual rule to choose begin and end functions for iterables is the following: use the class begin and end function if it has some. Use overloads of the global functions std::begin and std::end if some are provided.

    Static arrays not being class/struct, they don’t/can’t have member functions. The functions called by the foreach loop are the global functions std::begin and std::end, taking an array as parameter. Assuming std::rbegin and std::rend existed, you would have to construct your wrapper the following way:

    template <typename Iterable>
    struct ReverseWrapper {
    private:
      Iterable& m_iterable;
    
    public:
      ReverseWrapper(Iterable&& iterable) : m_iterable(iterable) {}
    
      auto begin() const -> decltype(rbegin(m_iterable)) {
        return rbegin(m_iterable);
      }
    
      auto end() const -> decltype(rend(m_iterable)) {
        return rend(m_iterable);
      }
    };
    
    template<typename Iterable>
    auto reverseIterate(Iterable&& list)
        -> ReverseWrapper<Iterable>
    {
        return ReverseWrapper<Iterable>(std::forward<Iterable>(list));
    }
    

    Even though std::rbegin and std::rend exist in the c++14 standard, they are not available in the c++11 one. So, to get the above code to work with c++11, you would have to implement these functions by hand:

    template<typename T, std::size_t N>
    auto rbegin(T (&array)[N])
        -> std::reverse_iterator<T*>
    {
        return std::reverse_iterator<T*>(std::end(array));
    }
    
    template<typename T, std::size_t N>
    auto rend(T (&array)[N])
        -> std::reverse_iterator<T*>
    {
        return std::reverse_iterator<T*>(std::begin(array));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

To help me better understand lambda I wrote this short snippet that rotates and
I'm trying to write a short function that will let me quickly read in
I wrote a short script that never terminates. This script continuously generates output that
This is a weird one. Long story short: wrote a usercontrol using AJAX. Used
I wrote this short program to learn the javax.sound.midi system. This is using Java
I want to sort an array by using uasort() function. I wrote this piece
I wrote a short bash script to complete a task that involves creating a
I've wrote an App that streams a short movie over a fix URL. Everything
I wrote this function that creates a random string of UTF-8 characters. It works
I wrote this short program which has a tiny GUI. Its supposed to allow

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.