From 065cf3f9d70374d0a02f1e15be32eaaa59f01466 Mon Sep 17 00:00:00 2001 From: zoecarver Date: Tue, 1 Jun 2021 12:55:33 -0700 Subject: [PATCH] [libcxx][ranges] Add `default_sentinel` and `default_sentinel_t`. Refs https://eel.is/c++draft/default.sentinel and https://eel.is/c++draft/iterator.synopsis Differential Revision: https://reviews.llvm.org/D103487 --- libcxx/docs/OneRangesProposalStatus.csv | 2 +- libcxx/include/CMakeLists.txt | 1 + libcxx/include/__iterator/default_sentinel.h | 35 +++++++++++++++++++ libcxx/include/iterator | 4 +++ .../default.sentinel.pass.cpp | 34 ++++++++++++++++++ 5 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 libcxx/include/__iterator/default_sentinel.h create mode 100644 libcxx/test/std/iterators/predef.iterators/default.sentinel/default.sentinel.pass.cpp diff --git a/libcxx/docs/OneRangesProposalStatus.csv b/libcxx/docs/OneRangesProposalStatus.csv index 0917282c70dd..95e3030f6e29 100644 --- a/libcxx/docs/OneRangesProposalStatus.csv +++ b/libcxx/docs/OneRangesProposalStatus.csv @@ -31,7 +31,7 @@ bidirectional_iterator: `D100278 `_", [predef.iterators],Updates to predefined iterators.,"[iterator.concepts], [iterator.cust.swap], [iterator.cust.move]",,, [move.sentinel],,[concepts] … Note: for testing it may be beneficial to have completed [predef.iterators]. ,,, [common.iterator],,"[iterator.concepts], [iterator.cust.swap], [iterator.cust.move]",Zoe Carver,, -[default.sentinels],The empty std::default_sentinel_t.,,,, +[default.sentinels],The empty std::default_sentinel_t.,,Zoe Carver,,✅ [counted.iterator],,"[iterator.concepts], [iterator.cust.swap], [iterator.cust.move], [default.sentinels]",,, [stream.iterators],,[default.sentinels],,, [ranges.syn]: pt. 1,All the stuff not specified elsewhere. ,"[range.access], [iterator.concepts], [range.all], [range.subrange], unreachable, [range.empty]",,, diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt index 89b996671b23..b0c7b47958f3 100644 --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -16,6 +16,7 @@ set(files __hash_table __iterator/advance.h __iterator/concepts.h + __iterator/default_sentinel.h __iterator/incrementable_traits.h __iterator/indirect_concepts.h __iterator/iter_move.h diff --git a/libcxx/include/__iterator/default_sentinel.h b/libcxx/include/__iterator/default_sentinel.h new file mode 100644 index 000000000000..934a56fd9e29 --- /dev/null +++ b/libcxx/include/__iterator/default_sentinel.h @@ -0,0 +1,35 @@ +// -*- C++ -*- +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef _LIBCPP___ITERATOR_DEFAULT_SENTINEL_H +#define _LIBCPP___ITERATOR_DEFAULT_SENTINEL_H + +#include <__config> + +#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) +#pragma GCC system_header +#endif + +_LIBCPP_PUSH_MACROS +#include <__undef_macros> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#if !defined(_LIBCPP_HAS_NO_RANGES) + +struct default_sentinel_t { }; +inline constexpr default_sentinel_t default_sentinel{}; + +#endif // !defined(_LIBCPP_HAS_NO_RANGES) + +_LIBCPP_END_NAMESPACE_STD + +_LIBCPP_POP_MACROS + +#endif // _LIBCPP___ITERATOR_DEFAULT_SENTINEL_H diff --git a/libcxx/include/iterator b/libcxx/include/iterator index 364c74325b18..9d9a5532a2c6 100644 --- a/libcxx/include/iterator +++ b/libcxx/include/iterator @@ -383,6 +383,9 @@ constexpr move_iterator operator+( // constexpr in C++17 template // constexpr in C++17 constexpr move_iterator make_move_iterator(const Iterator& i); +// [default.sentinel], default sentinel +struct default_sentinel_t; +inline constexpr default_sentinel_t default_sentinel{}; template , class Distance = ptrdiff_t> class istream_iterator @@ -554,6 +557,7 @@ template constexpr const E* data(initializer_list il) noexcept; #include <__functional_base> #include <__iterator/advance.h> #include <__iterator/concepts.h> +#include <__iterator/default_sentinel.h> #include <__iterator/incrementable_traits.h> #include <__iterator/indirect_concepts.h> #include <__iterator/iter_move.h> diff --git a/libcxx/test/std/iterators/predef.iterators/default.sentinel/default.sentinel.pass.cpp b/libcxx/test/std/iterators/predef.iterators/default.sentinel/default.sentinel.pass.cpp new file mode 100644 index 000000000000..8c8487950f28 --- /dev/null +++ b/libcxx/test/std/iterators/predef.iterators/default.sentinel/default.sentinel.pass.cpp @@ -0,0 +1,34 @@ +//===----------------------------------------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +// UNSUPPORTED: c++03, c++11, c++14, c++17 +// UNSUPPORTED: libcpp-no-concepts +// UNSUPPORTED: gcc-10 + +// struct default_sentinel_t; +// inline constexpr default_sentinel_t default_sentinel; + +#include + +#include +#include + +#include "test_macros.h" + +int main(int, char**) { + static_assert(std::is_empty_v); + static_assert(std::semiregular); + + static_assert(std::same_as); + + std::default_sentinel_t s1; + auto s2 = std::default_sentinel_t{}; + s2 = s1; + + return 0; +}