klmr/cpp11-range

Ranges with different types

Closed this issue · 1 comments

Hi,
I think that your work is great and really helps to improve C++ beauty!
However, the behavior of range seems a little bit buggy to me, in some circumstances:

#include <iostream>
#include "range.hpp"

using util::lang::range;

template <size_t N>
struct Foo
{
    void loops()
    {
        for (const auto i : range(0, N)) // compile error here:  'no matching function for call to 'range(int, long unsigned int)'
            std::cout << i << " ";
    }
};

And is very annoying.

Obviously, substituting the loop with:
for (const auto i : range(static_cast<decltype(N)>(0), N))
It works but we care about beauty!

Part of the solution could be declare something like this:

template <typename T, typename S>
range_proxy<S> range(T begin, S end) {
    return {S(begin), end};
}
// probably some enable_if such as std::is_constructible<T, S>::value && std::is_convertible<S, T>::value is needed

What do you think about this problem?

Thanks in advance!

klmr commented

Yes, this is a very good idea. I don’t think the std::is_constructible && std::is_convertible checks are needed though; if the conversion fails, we’ll get an error message anyway, and the error messages on modern compilers are going to be quite similar. Concepts might change this answer but this code is C++11.