//===----------------------------------------------------------------------===// // // 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 // // template class optional::iterator; // template class optional::const_iterator; #include #include #include #include #include #include template constexpr bool test() { std::optional opt{__val}; { // Dereferencing an iterator of an engaged optional will return the same value that the optional holds. auto it = opt.begin(); auto it2 = std::as_const(opt).begin(); assert(*it == *opt); assert(*it2 == *std::as_const(opt)); } { // optional::iterator and optional::const_iterator satisfy the Cpp17RandomAccessIterator and contiguous iterator. auto it = opt.begin(); auto it2 = std::as_const(opt).begin(); assert(std::contiguous_iterator); assert(std::contiguous_iterator); assert(std::random_access_iterator); assert(std::random_access_iterator); } { // const_iterator::value_type == std::remove_cv_t, const_iterator::reference == const T&, iterator::value_type = std::remove_cv_t, iterator::reference == T& auto it = opt.begin(); auto it2 = std::as_const(opt).begin(); assert((std::is_same_v>)); assert((std::is_same_v)); assert((std::is_same_v>)); assert((std::is_same_v)); } { // std::ranges::size for an engaged optional == 1, disengaged optional == 0 const std::optional disengaged{std::nullopt}; std::optional disengaged2{std::nullopt}; assert(std::ranges::size(opt) == 1); assert(std::ranges::size(std::as_const(opt)) == 1); assert(std::ranges::size(disengaged) == 0); assert(std::ranges::size(disengaged2) == 0); } { // std::ranges::enable_view> == true, and std::format_kind> == true static_assert(std::ranges::enable_view> == true); static_assert(std::format_kind> == std::range_format::disabled); } // An optional with value that is reset will have a begin() == end(), then when it is reassigned a value, // begin() != end(), and *begin() will contain the new value. { std::optional val{__val}; assert(val.begin() != val.end()); val.reset(); assert(val.begin() == val.end()); val.emplace(__val); assert(val.begin() != val.end()); assert(*(val.begin()) == __val); } return true; } constexpr bool tests() { assert((test())); assert((test())); assert((test())); assert((test())); assert((test())); return true; } int main(int, char**) { assert(tests()); static_assert(tests()); return 0; }