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 // NOTE: Older versions of clang have a bug where they fail to evaluate 10 // string_view::at as a constant expression. 11 // XFAIL: clang-3.4, clang-3.3 12 13 14 // <string_view> 15 16 // constexpr const _CharT& at(size_type _pos) const; 17 18 #include <string_view> 19 #include <stdexcept> 20 #include <cassert> 21 22 #include "test_macros.h" 23 24 template <typename CharT> 25 void test ( const CharT *s, size_t len ) { 26 std::basic_string_view<CharT> sv ( s, len ); 27 assert ( sv.length() == len ); 28 for ( size_t i = 0; i < len; ++i ) { 29 assert ( sv.at(i) == s[i] ); 30 assert ( &sv.at(i) == s + i ); 31 } 32 33 #ifndef TEST_HAS_NO_EXCEPTIONS 34 try { (void)sv.at(len); } catch ( const std::out_of_range & ) { return ; } 35 assert ( false ); 36 #endif 37 } 38 39 int main(int, char**) { 40 test ( "ABCDE", 5 ); 41 test ( "a", 1 ); 42 43 test ( L"ABCDE", 5 ); 44 test ( L"a", 1 ); 45 46 #if TEST_STD_VER >= 11 47 test ( u"ABCDE", 5 ); 48 test ( u"a", 1 ); 49 50 test ( U"ABCDE", 5 ); 51 test ( U"a", 1 ); 52 #endif 53 54 #if TEST_STD_VER >= 11 55 { 56 constexpr std::basic_string_view<char> sv ( "ABC", 2 ); 57 static_assert ( sv.length() == 2, "" ); 58 static_assert ( sv.at(0) == 'A', "" ); 59 static_assert ( sv.at(1) == 'B', "" ); 60 } 61 #endif 62 63 return 0; 64 } 65