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// UNSUPPORTED: c++03, c++11, c++14
10
11// <iterator>
12// template <class C> constexpr auto data(C& c) -> decltype(c.data()); // C++17
13// template <class C> constexpr auto data(const C& c) -> decltype(c.data()); // C++17
14// template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept; // C++17
15// template <class E> constexpr const E* data(initializer_list<E> il) noexcept; // C++17
16
17#include <iterator>
18#include <cassert>
19#include <vector>
20#include <array>
21#include <initializer_list>
22
23#include "test_macros.h"
24
25#if TEST_STD_VER > 14
26#include <string_view>
27#endif
28
29template<typename C>
30void test_const_container( const C& c )
31{
32// Can't say noexcept here because the container might not be
33 assert ( std::data(c) == c.data());
34}
35
36template<typename T>
37void test_const_container( const std::initializer_list<T>& c )
38{
39 ASSERT_NOEXCEPT(std::data(c));
40 assert ( std::data(c) == c.begin());
41}
42
43template<typename C>
44void test_container( C& c )
45{
46// Can't say noexcept here because the container might not be
47 assert ( std::data(c) == c.data());
48}
49
50template<typename T>
51void test_container( std::initializer_list<T>& c)
52{
53 ASSERT_NOEXCEPT(std::data(c));
54 assert ( std::data(c) == c.begin());
55}
56
57template<typename T, std::size_t Sz>
58void test_const_array( const T (&array)[Sz] )
59{
60 ASSERT_NOEXCEPT(std::data(array));
61 assert ( std::data(array) == &array[0]);
62}
63
64int main(int, char**)
65{
66 std::vector<int> v; v.push_back(x: 1);
67 std::array<int, 1> a; a[0] = 3;
68 std::initializer_list<int> il = { 4 };
69
70 test_container ( c&: v );
71 test_container ( c&: a );
72 test_container ( c&: il );
73
74 test_const_container ( c: v );
75 test_const_container ( c: a );
76 test_const_container ( c: il );
77
78#if TEST_STD_VER > 14
79 std::string_view sv{"ABC"};
80 test_container ( sv );
81 test_const_container ( sv );
82#endif
83
84 static constexpr int arrA [] { 1, 2, 3 };
85 test_const_array ( array: arrA );
86
87 return 0;
88}
89

source code of libcxx/test/std/iterators/iterator.container/data.pass.cpp