Home | History | Annotate | Download | only in array
      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 // <array>
     11 
     12 // reference operator[] (size_type)
     13 // const_reference operator[] (size_type); // constexpr in C++14
     14 // reference at (size_type)
     15 // const_reference at (size_type); // constexpr in C++14
     16 
     17 #include <array>
     18 #include <cassert>
     19 
     20 int main()
     21 {
     22     {
     23         typedef double T;
     24         typedef std::array<T, 3> C;
     25         C c = {1, 2, 3.5};
     26         C::reference r1 = c.at(0);
     27         assert(r1 == 1);
     28         r1 = 5.5;
     29         assert(c.front() == 5.5);
     30 
     31         C::reference r2 = c.at(2);
     32         assert(r2 == 3.5);
     33         r2 = 7.5;
     34         assert(c.back() == 7.5);
     35 
     36         try { (void) c.at(3); }
     37         catch (const std::out_of_range &) {}
     38     }
     39     {
     40         typedef double T;
     41         typedef std::array<T, 3> C;
     42         const C c = {1, 2, 3.5};
     43         C::const_reference r1 = c.at(0);
     44         assert(r1 == 1);
     45 
     46         C::const_reference r2 = c.at(2);
     47         assert(r2 == 3.5);
     48 
     49         try { (void) c.at(3); }
     50         catch (const std::out_of_range &) {}
     51     }
     52 
     53 #if _LIBCPP_STD_VER > 11
     54     {
     55         typedef double T;
     56         typedef std::array<T, 3> C;
     57         constexpr C c = {1, 2, 3.5};
     58 
     59         constexpr T t1 = c.at(0);
     60         static_assert (t1 == 1, "");
     61 
     62         constexpr T t2 = c.at(2);
     63         static_assert (t2 == 3.5, "");
     64     }
     65 #endif
     66 
     67 }
     68