-
Notifications
You must be signed in to change notification settings - Fork 14.4k
WIP [libc++][ranges] P3060R3: Add std::views::indices(n) #146823
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
base: main
Are you sure you want to change the base?
Changes from 6 commits
251a813
5e9293a
e2c569c
aacc52c
207fb0c
0f2b6cd
cc3f700
c1981bd
8e48a6c
0c947c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -392,6 +392,16 @@ struct __fn { | |
inline namespace __cpo { | ||
inline constexpr auto iota = __iota::__fn{}; | ||
} // namespace __cpo | ||
|
||
|
||
# if _LIBCPP_STD_VER >= 26 | ||
|
||
inline constexpr auto indices = [](__integer_like auto __size) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Also, is there any particular reason not to make this lambda There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am not sure about that. Why would we want to mark a CPO lambda There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be clear, I'm inclined to make the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! |
||
return ranges::views::iota(decltype(__size){}, __size); | ||
}; | ||
|
||
# endif | ||
|
||
} // namespace views | ||
} // namespace ranges | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// REQUIRES: std-at-least-c++26 | ||
|
||
// ranges | ||
|
||
// inline constexpr unspecified indices = unspecified; | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <ranges> | ||
#include <vector> | ||
|
||
#include "test_macros.h" | ||
#define TEST_HAS_NO_INT128 // Size cannot be larger than 64 bits | ||
#include "type_algorithms.h" | ||
|
||
#include "types.h" | ||
|
||
// Test SFINAE. | ||
|
||
template <typename SizeType> | ||
concept HasIndices = requires(SizeType s) { std::ranges::views::indices(s); }; | ||
|
||
struct IntegerTypesTest { | ||
template <class T> | ||
constexpr void operator()() { | ||
static_assert(HasIndices<T>); | ||
} | ||
}; | ||
|
||
struct NotIntegerLike {}; | ||
|
||
void test_SFIANE() { | ||
static_assert(HasIndices<std::size_t>); | ||
types::for_each(types::integer_types(), IntegerTypesTest{}); | ||
|
||
// Not integer-like types should not satisfy HasIndices | ||
static_assert(!HasIndices<bool>); | ||
static_assert(!HasIndices<float>); | ||
static_assert(!HasIndices<void>); | ||
static_assert(!HasIndices<SomeInt>); // Does satisfy is_integer_like, but not the conversion to std::size_t | ||
static_assert(!HasIndices<NotIntegerLike>); | ||
} | ||
|
||
constexpr bool test() { | ||
{ | ||
auto indices_view = std::ranges::views::indices(5); | ||
assert(indices_view.size() == 5); | ||
|
||
// Check that the view is a range | ||
static_assert(std::ranges::range<decltype(indices_view)>); | ||
|
||
assert(indices_view[0] == 0); | ||
assert(indices_view[1] == 1); | ||
assert(indices_view[2] == 2); | ||
assert(indices_view[3] == 3); | ||
assert(indices_view[4] == 4); | ||
} | ||
|
||
{ | ||
std::vector v(5, 0); | ||
|
||
auto indices_view = std::ranges::views::indices(std::ranges::size(v)); | ||
assert(indices_view.size() == 5); | ||
|
||
// Check that the view is a range | ||
static_assert(std::ranges::range<decltype(indices_view)>); | ||
|
||
assert(indices_view[0] == 0); | ||
assert(indices_view[1] == 1); | ||
assert(indices_view[2] == 2); | ||
assert(indices_view[3] == 3); | ||
assert(indices_view[4] == 4); | ||
} | ||
|
||
{ | ||
std::vector v(5, SomeInt{}); | ||
|
||
// Check that the indices view works as expected | ||
auto indices_view = std::ranges::views::indices(std::ranges::size(v)); | ||
assert(indices_view.size() == 5); | ||
|
||
// Check that the view is a range | ||
static_assert(std::ranges::range<decltype(indices_view)>); | ||
|
||
assert(indices_view[0] == 0); | ||
assert(indices_view[1] == 1); | ||
assert(indices_view[2] == 2); | ||
assert(indices_view[3] == 3); | ||
assert(indices_view[4] == 4); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
int main(int, char**) { | ||
test_SFIANE(); | ||
|
||
test(); | ||
static_assert(test()); | ||
|
||
return 0; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.