Skip to content

Commit a772b52

Browse files
committed
Mark vector<bool>::at() as constexpr
1 parent 737d6ca commit a772b52

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

libcxx/include/__vector/vector_bool.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,8 +279,8 @@ class _LIBCPP_TEMPLATE_VIS vector<bool, _Allocator> {
279279
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference operator[](size_type __n) const {
280280
return __make_ref(__n);
281281
}
282-
_LIBCPP_HIDE_FROM_ABI reference at(size_type __n);
283-
_LIBCPP_HIDE_FROM_ABI const_reference at(size_type __n) const;
282+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference at(size_type __n);
283+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference at(size_type __n) const;
284284

285285
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 reference front() { return __make_ref(0); }
286286
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX20 const_reference front() const { return __make_ref(0); }
@@ -853,14 +853,15 @@ _LIBCPP_CONSTEXPR_SINCE_CXX20 void vector<bool, _Allocator>::shrink_to_fit() _NO
853853
}
854854

855855
template <class _Allocator>
856-
typename vector<bool, _Allocator>::reference vector<bool, _Allocator>::at(size_type __n) {
856+
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::reference vector<bool, _Allocator>::at(size_type __n) {
857857
if (__n >= size())
858858
this->__throw_out_of_range();
859859
return (*this)[__n];
860860
}
861861

862862
template <class _Allocator>
863-
typename vector<bool, _Allocator>::const_reference vector<bool, _Allocator>::at(size_type __n) const {
863+
_LIBCPP_CONSTEXPR_SINCE_CXX20 typename vector<bool, _Allocator>::const_reference
864+
vector<bool, _Allocator>::at(size_type __n) const {
864865
if (__n >= size())
865866
this->__throw_out_of_range();
866867
return (*this)[__n];
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+
// constexpr const_reference at(size_type n) const;
12+
// constexpr reference at(size_type n);
13+
14+
#include <cassert>
15+
#include <memory>
16+
#include <vector>
17+
18+
#include "min_allocator.h"
19+
#include "test_allocator.h"
20+
21+
template <typename T, typename Allocator>
22+
constexpr void test() {
23+
std::vector<T, Allocator> v{1, 0, 1};
24+
assert(v.at(0) == 1);
25+
assert(v.at(1) == 0);
26+
assert(v.at(2) == 1);
27+
}
28+
29+
constexpr bool tests() {
30+
test<bool, std::allocator<bool>>();
31+
test<bool, min_allocator<bool>>();
32+
test<bool, test_allocator<bool>>();
33+
34+
return true;
35+
}
36+
37+
int main(int, char**) {
38+
tests();
39+
static_assert(tests());
40+
41+
return 0;
42+
}

0 commit comments

Comments
 (0)