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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:49:40+00:00 2026-05-14T01:49:40+00:00

How would you fix this code? template <typename T> void closed_range(T begin, T end)

  • 0

How would you fix this code?

template <typename T> void closed_range(T begin, T end)
{
    for (T i = begin; i <= end; ++i) {
        // do something
    }
}
  • T is constrained to be an integer type, can be the wider of such types and can be signed or unsigned

  • begin can be numeric_limits<T>::min()

  • end can be numeric_limits<T>::max() (in which case ++i will overflow in the above code)

I’ve several ways, but none I really like.

  • 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-14T01:49:40+00:00Added an answer on May 14, 2026 at 1:49 am

    Maybe,

    template <typename T> void closed_range(T begin, const T end)
        if (begin <= end) {
            do {
                // do something
            } while (begin != end && (++begin, true));
        }
    }
    

    Curses, my first attempt was wrong, and the fix above isn’t as pretty as I’d hoped. How about:

    template <typename T> bool advance(T &value) { ++value; return true; }
    
    template <typename T> void closed_range(T first, const T last)
        if (first <= last) {
            do {
                // do something
            } while (first != last && advance(first));
        }
    }
    

    There’s no ambiguity with std::advance even if T isn’t an integer type, since std::advance takes 2 parameters. So the template would also work with for instance a random-access iterator, if for some reason you wanted a closed range of those.

    Or how about a bit of set theory? Obviously this is massive overkill if you’re only writing one loop over a closed range, but if it’s something that you want to do a lot, then it makes the loop code about right. Not sure about efficiency: in a really tight loop you might want make sure the call to endof is hoisted:

    #include <limits>
    #include <iostream>
    
    template <typename T>
    struct omega {
        T val;
        bool isInfinite;
        operator T() { return val; }
        explicit omega(const T &v) : val(v), isInfinite(false) { }
        omega &operator++() {
            (val == std::numeric_limits<T>::max()) ? isInfinite = true : ++val;
            return *this;
        }
    };
    
    template <typename T>
    bool operator==(const omega<T> &lhs, const omega<T> &rhs) {
        if (lhs.isInfinite) return rhs.isInfinite;
        return (!rhs.isInfinite) && lhs.val == rhs.val;
    }
    template <typename T>
    bool operator!=(const omega<T> &lhs, const omega<T> &rhs) {
        return !(lhs == rhs);
    }
    
    template <typename T>
    omega<T> endof(T val) { 
        omega<T> e(val);
        return ++e;
    }
    
    template <typename T>
    void closed_range(T first, T last) {
        for (omega<T> i(first); i != endof(last); ++i) {
            // do something
            std::cout << i << "\n";
        }
    }
    
    int main() {
        closed_range((short)32765, std::numeric_limits<short>::max());
        closed_range((unsigned short)65533, std::numeric_limits<unsigned short>::max());
        closed_range(1, 0);
    }
    

    Output:

    32765
    32766
    32767
    65533
    65534
    65535
    

    Be a bit careful using other operators on omega<T> objects. I’ve only implemented the absolute minimum for the demonstration, and omega<T> implicitly converts to T, so you’ll find that you can write expressions which potentially throw away the “infiniteness” of omega objects. You could fix that by declaring (not necessarily defining) a full set of arithmetic operators; or by throwing an exception in the conversion if isInfinite is true; or just don’t worry about it on grounds that you can’t accidentally convert the result back to an omega, because the constructor is explicit. But for example, omega<int>(2) < endof(2) is true, but omega<int>(INT_MAX) < endof(INT_MAX) is false.

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

Sidebar

Ask A Question

Stats

  • Questions 391k
  • Answers 391k
  • 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 Finally, I've used IO.DLL, which unfortunately doesn't work on 64-bit,… May 15, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer I think the "val0, val1.." are not getting replaced with… May 15, 2026 at 1:19 am
  • Editorial Team
    Editorial Team added an answer Create the recipes and ingredients, then for each ingredient, create… May 15, 2026 at 1:19 am

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.