Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ranges with different types #8

Closed
dellorogiulio opened this issue Nov 20, 2020 · 1 comment
Closed

Ranges with different types #8

dellorogiulio opened this issue Nov 20, 2020 · 1 comment

Comments

@dellorogiulio
Copy link

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
Copy link
Owner

klmr commented Nov 21, 2020

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.

klmr added a commit that referenced this issue Nov 21, 2020
@klmr klmr closed this as completed Nov 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants