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