xref: /llvm-project/libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp (revision 85d4e29fd84e1afe03a589e240e33aa5a5c4b7ce)
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 // UNSUPPORTED: c++03, c++11, c++14, c++17
12 
13 // template <class T> constexpr T* to_address(T* p) noexcept;
14 // template <class Ptr> constexpr auto to_address(const Ptr& p) noexcept;
15 
16 #include <memory>
17 
18 #include <array>
19 #include <cassert>
20 #include <span>
21 #include <string>
22 #include <string_view>
23 #include <valarray>
24 #include <vector>
25 #include "test_macros.h"
26 
27 template<class C>
test_container_iterators(C c)28 void test_container_iterators(C c)
29 {
30     const C& cc = c;
31     assert(std::to_address(c.begin()) == c.data());
32     assert(std::to_address(c.end()) == c.data() + c.size());
33     assert(std::to_address(cc.begin()) == cc.data());
34     assert(std::to_address(cc.end()) == cc.data() + cc.size());
35 }
36 
test_valarray_iterators()37 void test_valarray_iterators()
38 {
39     std::valarray<int> v(100);
40     int *p = std::to_address(std::begin(v));
41     int *q = std::to_address(std::end(v));
42     assert(q - p == 100);
43 }
44 
main(int,char **)45 int main(int, char**) {
46     test_container_iterators(std::array<int, 3>());
47     test_container_iterators(std::vector<int>(3));
48     test_container_iterators(std::string("abc"));
49     test_container_iterators(std::string_view("abc"));
50     test_container_iterators(std::span<const char>("abc"));
51     test_valarray_iterators();
52 
53     return 0;
54 }
55