xref: /llvm-project/libcxx/test/std/iterators/iterator.container/data.pass.cpp (revision 5f8d84ec924900b337f151f7fc5a77620f25a932)
1 //===----------------------------------------------------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 // UNSUPPORTED: c++98, c++03, c++11, c++14
11 
12 // <iterator>
13 // template <class C> constexpr auto data(C& c) -> decltype(c.data());               // C++17
14 // template <class C> constexpr auto data(const C& c) -> decltype(c.data());         // C++17
15 // template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;           // C++17
16 // template <class E> constexpr const E* data(initializer_list<E> il) noexcept;      // C++17
17 
18 #include <iterator>
19 #include <cassert>
20 #include <vector>
21 #include <array>
22 #include <initializer_list>
23 
24 #include "test_macros.h"
25 
26 template<typename C>
27 void test_const_container( const C& c )
28 {
29 //  Can't say noexcept here because the container might not be
30     assert ( std::data(c)   == c.data());
31 }
32 
33 template<typename T>
34 void test_const_container( const std::initializer_list<T>& c )
35 {
36     ASSERT_NOEXCEPT(std::data(c));
37     assert ( std::data(c)   == c.begin());
38 }
39 
40 template<typename C>
41 void test_container( C& c )
42 {
43 //  Can't say noexcept here because the container might not be
44     assert ( std::data(c)   == c.data());
45 }
46 
47 template<typename T>
48 void test_container( std::initializer_list<T>& c)
49 {
50     ASSERT_NOEXCEPT(std::data(c));
51     assert ( std::data(c)   == c.begin());
52 }
53 
54 template<typename T, size_t Sz>
55 void test_const_array( const T (&array)[Sz] )
56 {
57     ASSERT_NOEXCEPT(std::data(array));
58     assert ( std::data(array) == &array[0]);
59 }
60 
61 int main()
62 {
63     std::vector<int> v; v.push_back(1);
64     std::array<int, 1> a; a[0] = 3;
65     std::initializer_list<int> il = { 4 };
66 
67     test_container ( v );
68     test_container ( a );
69     test_container ( il );
70 
71     test_const_container ( v );
72     test_const_container ( a );
73     test_const_container ( il );
74 
75     static constexpr int arrA [] { 1, 2, 3 };
76     test_const_array ( arrA );
77 }
78