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

What's the appropriate approach to #1157? #1978

Closed
SainoNamkho opened this issue Oct 3, 2022 · 3 comments
Closed

What's the appropriate approach to #1157? #1978

SainoNamkho opened this issue Oct 3, 2022 · 3 comments

Comments

@SainoNamkho
Copy link

SainoNamkho commented Oct 3, 2022

Suppose I'm writing an insertion sort

void insertSort(span<int> sp)
{
    for (int i = 0; i < sp.size(); ++i) {
        auto insert_pos = ranges::upper_bound(sp.first(i), sp[i]);
        if (insert_pos == sp.first(i).end()) {
            int temp = sp[i];
            ranges::copy(span{insert_pos, sp.begin()+i}, insert_pos+1);
            *insert_pos = temp;
        }
    }
}

It's wrong since insert_pos, sp.first(i).end() and sp.begin() are from three different span objects.
#1157 suggest to do this with ranges.

void insertSort(span<int> sp)
{
    for (auto it = sp.begin(); it != sp.end(); ++it) {
        auto insert_pos = ranges::upper_bound(ranges::subrange{sp.begin(), it}, *it);
        if (insert_pos == it) {
            int temp = *it;
            ranges::copy(ranges::subrange{insert_pos, it}, insert_pos+1);
            *insert_pos = temp;
        }
    }
}

But four years since that issue, clang still cannot compile it. llvm/llvm-project#52696 says it's fixed, while actually it just do not compile.

So what are we supposed to do? Just sit down and wait for clang 15 to fix it?

@jwakely
Copy link
Contributor

jwakely commented Oct 3, 2022

Yes, or use a different compiler.

The future is already here, it's just not evenly distributed.

@jwakely
Copy link
Contributor

jwakely commented Oct 3, 2022

You can also convert the span iterators to pointers and compare those, so that any debug checking on the iterators doesn't complain about iterators to different ranges.

@SainoNamkho
Copy link
Author

SainoNamkho commented Oct 3, 2022

You can also convert the span iterators to pointers and compare those, so that any debug checking on the iterators doesn't complain about iterators to different ranges.

Thanks a lot, maybe I'll go back to the original unconstrained algorithms, since such convertion needs care of subspan's past the end iterator.

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