1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <memory>
10 
11 // template <class T> constexpr T* __to_address(T* p) noexcept;
12 // template <class Ptr> constexpr auto __to_address(const Ptr& p) noexcept;
13 
14 #include <memory>
15 
16 #include <array>
17 #include <cassert>
18 #include <span>
19 #include <string>
20 #include <string_view>
21 #include <valarray>
22 #include <vector>
23 #include "test_macros.h"
24 
25 template<class C>
test_container_iterators(C c)26 void test_container_iterators(C c)
27 {
28     const C& cc = c;
29     assert(std::__to_address(c.begin()) == c.data());
30     assert(std::__to_address(c.end()) == c.data() + c.size());
31     assert(std::__to_address(cc.begin()) == cc.data());
32     assert(std::__to_address(cc.end()) == cc.data() + cc.size());
33 }
34 
test_valarray_iterators()35 void test_valarray_iterators()
36 {
37     std::valarray<int> v(100);
38     int *p = std::__to_address(std::begin(v));
39     int *q = std::__to_address(std::end(v));
40     assert(q - p == 100);
41 }
42 
main(int,char **)43 int main(int, char**) {
44     test_container_iterators(std::array<int, 3>());
45     test_container_iterators(std::vector<int>(3));
46     test_container_iterators(std::string("abc"));
47 #if TEST_STD_VER >= 17
48     test_container_iterators(std::string_view("abc"));
49 #endif
50 #if TEST_STD_VER >= 20
51     test_container_iterators(std::span<const char>("abc"));
52 #endif
53     test_valarray_iterators();
54 
55     return 0;
56 }
57