Home | History | Annotate | Download | only in iterator.container
      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 // <iterator>
     11 // template <class C> constexpr auto data(C& c) -> decltype(c.data());               // C++17
     12 // template <class C> constexpr auto data(const C& c) -> decltype(c.data());         // C++17
     13 // template <class T, size_t N> constexpr T* data(T (&array)[N]) noexcept;           // C++17
     14 // template <class E> constexpr const E* data(initializer_list<E> il) noexcept;      // C++17
     15 
     16 #include "test_macros.h"
     17 
     18 #if TEST_STD_VER <= 14
     19 int main () {}
     20 #else
     21 
     22 #include <iterator>
     23 #include <cassert>
     24 #include <vector>
     25 #include <array>
     26 #include <initializer_list>
     27 
     28 template<typename C>
     29 void test_const_container( const C& c )
     30 {
     31     assert ( std::data(c)   == c.data());
     32 }
     33 
     34 template<typename T>
     35 void test_const_container( const std::initializer_list<T>& c )
     36 {
     37     assert ( std::data(c)   == c.begin());
     38 }
     39 
     40 template<typename C>
     41 void test_container( C& c )
     42 {
     43     assert ( std::data(c)   == c.data());
     44 }
     45 
     46 template<typename T>
     47 void test_container( std::initializer_list<T>& c)
     48 {
     49     assert ( std::data(c)   == c.begin());
     50 }
     51 
     52 template<typename T, size_t Sz>
     53 void test_const_array( const T (&array)[Sz] )
     54 {
     55     assert ( std::data(array) == &array[0]);
     56 }
     57 
     58 int main()
     59 {
     60     std::vector<int> v; v.push_back(1);
     61     std::array<int, 1> a; a[0] = 3;
     62     std::initializer_list<int> il = { 4 };
     63 
     64     test_container ( v );
     65     test_container ( a );
     66     test_container ( il );
     67 
     68     test_const_container ( v );
     69     test_const_container ( a );
     70     test_const_container ( il );
     71 
     72     static constexpr int arrA [] { 1, 2, 3 };
     73     test_const_array ( arrA );
     74 }
     75 
     76 #endif
     77