//===----------------------------------------------------------------------===// // // 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 // //===----------------------------------------------------------------------===// // // All of these became constexpr in C++17 // // template // constexpr void advance(Iter& i, Distance n); // // template // constexpr void advance(Iter& i, Distance n); // // template // constexpr void advance(Iter& i, Distance n); // TODO: test_iterators.h includes , and includes and . // Lots of implementation headers under <__chrono/> and has signed to unsigned conversion, // which will trigger the -Wsign-conversion warning. // Once those headers are fixed, enable the -Wsign-conversion for this test by removing // below // Make sure we catch forced conversions to the difference_type if they happen. // ADDITIONAL_COMPILE_FLAGS: -Wsign-conversion #include #include #include #include #include "test_macros.h" #include "test_iterators.h" template TEST_CONSTEXPR_CXX17 void check_advance(It it, Distance n, It result) { static_assert(std::is_same::value, ""); std::advance(it, n); assert(it == result); } TEST_CONSTEXPR_CXX17 bool tests() { const char* s = "1234567890"; // Check with iterator_traits::difference_type { typedef std::iterator_traits::difference_type Distance; check_advance(cpp17_input_iterator(s), 10, cpp17_input_iterator(s+10)); check_advance(forward_iterator(s), 10, forward_iterator(s+10)); check_advance(bidirectional_iterator(s+5), 5, bidirectional_iterator(s+10)); check_advance(bidirectional_iterator(s+5), -5, bidirectional_iterator(s)); check_advance(random_access_iterator(s+5), 5, random_access_iterator(s+10)); check_advance(random_access_iterator(s+5), -5, random_access_iterator(s)); check_advance(s+5, 5, s+10); check_advance(s+5, -5, s); } // Also check with other distance types { typedef int Distance; check_advance(cpp17_input_iterator(s), 10, cpp17_input_iterator(s+10)); check_advance(forward_iterator(s), 10, forward_iterator(s+10)); check_advance(bidirectional_iterator(s), 10, bidirectional_iterator(s+10)); check_advance(random_access_iterator(s), 10, random_access_iterator(s+10)); } // Check with unsigned distance types to catch signedness-change issues { typedef std::size_t Distance; check_advance(cpp17_input_iterator(s), 10u, cpp17_input_iterator(s+10)); check_advance(forward_iterator(s), 10u, forward_iterator(s+10)); check_advance(bidirectional_iterator(s), 10u, bidirectional_iterator(s+10)); check_advance(random_access_iterator(s), 10u, random_access_iterator(s+10)); } return true; } int main(int, char**) { tests(); #if TEST_STD_VER >= 17 static_assert(tests(), ""); #endif return 0; }