[Clang] Don't form a type constraint if the concept is invalid (#122065)
[llvm-project.git] / libcxx / include / __cxx03 / __ranges / single_view.h
blob3e43302f213b164ff58f2ee84dbca52ae3d447af
1 // -*- C++ -*-
2 //===----------------------------------------------------------------------===//
3 //
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___CXX03___RANGES_SINGLE_VIEW_H
11 #define _LIBCPP___CXX03___RANGES_SINGLE_VIEW_H
13 #include <__cxx03/__concepts/constructible.h>
14 #include <__cxx03/__config>
15 #include <__cxx03/__ranges/movable_box.h>
16 #include <__cxx03/__ranges/range_adaptor.h>
17 #include <__cxx03/__ranges/view_interface.h>
18 #include <__cxx03/__type_traits/decay.h>
19 #include <__cxx03/__type_traits/is_object.h>
20 #include <__cxx03/__utility/forward.h>
21 #include <__cxx03/__utility/in_place.h>
22 #include <__cxx03/__utility/move.h>
23 #include <__cxx03/cstddef>
25 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
26 # pragma GCC system_header
27 #endif
29 _LIBCPP_PUSH_MACROS
30 #include <__cxx03/__undef_macros>
32 _LIBCPP_BEGIN_NAMESPACE_STD
34 #if _LIBCPP_STD_VER >= 20
36 namespace ranges {
37 # if _LIBCPP_STD_VER >= 23
38 template <move_constructible _Tp>
39 # else
40 template <copy_constructible _Tp>
41 # endif
42 requires is_object_v<_Tp>
43 class _LIBCPP_ABI_LLVM18_NO_UNIQUE_ADDRESS single_view : public view_interface<single_view<_Tp>> {
44 _LIBCPP_NO_UNIQUE_ADDRESS __movable_box<_Tp> __value_;
46 public:
47 _LIBCPP_HIDE_FROM_ABI single_view()
48 requires default_initializable<_Tp>
49 = default;
51 _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(const _Tp& __t)
52 # if _LIBCPP_STD_VER >= 23
53 requires copy_constructible<_Tp>
54 # endif
55 : __value_(in_place, __t) {
58 _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(_Tp&& __t) : __value_(in_place, std::move(__t)) {}
60 template <class... _Args>
61 requires constructible_from<_Tp, _Args...>
62 _LIBCPP_HIDE_FROM_ABI constexpr explicit single_view(in_place_t, _Args&&... __args)
63 : __value_{in_place, std::forward<_Args>(__args)...} {}
65 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* begin() noexcept { return data(); }
67 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* begin() const noexcept { return data(); }
69 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* end() noexcept { return data() + 1; }
71 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* end() const noexcept { return data() + 1; }
73 _LIBCPP_HIDE_FROM_ABI static constexpr bool empty() noexcept { return false; }
75 _LIBCPP_HIDE_FROM_ABI static constexpr size_t size() noexcept { return 1; }
77 _LIBCPP_HIDE_FROM_ABI constexpr _Tp* data() noexcept { return __value_.operator->(); }
79 _LIBCPP_HIDE_FROM_ABI constexpr const _Tp* data() const noexcept { return __value_.operator->(); }
82 template <class _Tp>
83 single_view(_Tp) -> single_view<_Tp>;
85 namespace views {
86 namespace __single_view {
88 struct __fn : __range_adaptor_closure<__fn> {
89 template <class _Range>
90 [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr auto operator()(_Range&& __range) const
91 noexcept(noexcept(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))))
92 -> decltype(single_view<decay_t<_Range&&>>(std::forward<_Range>(__range))) {
93 return single_view<decay_t<_Range&&>>(std::forward<_Range>(__range));
96 } // namespace __single_view
98 inline namespace __cpo {
99 inline constexpr auto single = __single_view::__fn{};
100 } // namespace __cpo
102 } // namespace views
103 } // namespace ranges
105 #endif // _LIBCPP_STD_VER >= 20
107 _LIBCPP_END_NAMESPACE_STD
109 _LIBCPP_POP_MACROS
111 #endif // _LIBCPP___CXX03___RANGES_SINGLE_VIEW_H