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
27template<class C>
28void 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
37void test_valarray_iterators()
38{
39 std::valarray<int> v(100);
40 int *p = std::to_address(ptr: std::begin(va&: v));
41 int *q = std::to_address(ptr: std::end(va&: v));
42 assert(q - p == 100);
43}
44
45int main(int, char**) {
46 test_container_iterators(c: std::array<int, 3>());
47 test_container_iterators(c: std::vector<int>(3));
48 test_container_iterators(c: std::string("abc"));
49 test_container_iterators(c: std::string_view("abc"));
50 test_container_iterators(std::span<const char>("abc"));
51 test_valarray_iterators();
52
53 return 0;
54}
55

source code of libcxx/test/std/utilities/memory/pointer.conversion/to_address_std_iterators.pass.cpp