mirror of
https://github.com/llvm-mirror/libcxx.git
synced 2025-10-24 03:32:35 +08:00
Add noexcept to operator[] for array and deque. This is an extension. We already do this for string and string_view. This should give better codegen inside of noexcept functions.
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@356209 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -190,9 +190,9 @@ struct _LIBCPP_TEMPLATE_VIS array
|
|||||||
|
|
||||||
// element access:
|
// element access:
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX14
|
||||||
reference operator[](size_type __n) {return __elems_[__n];}
|
reference operator[](size_type __n) _NOEXCEPT {return __elems_[__n];}
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
const_reference operator[](size_type __n) const {return __elems_[__n];}
|
const_reference operator[](size_type __n) const _NOEXCEPT {return __elems_[__n];}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n);
|
_LIBCPP_CONSTEXPR_AFTER_CXX14 reference at(size_type __n);
|
||||||
_LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
|
_LIBCPP_CONSTEXPR_AFTER_CXX11 const_reference at(size_type __n) const;
|
||||||
@@ -303,13 +303,13 @@ struct _LIBCPP_TEMPLATE_VIS array<_Tp, 0>
|
|||||||
|
|
||||||
// element access:
|
// element access:
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
reference operator[](size_type) {
|
reference operator[](size_type) _NOEXCEPT {
|
||||||
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
|
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
|
||||||
_LIBCPP_UNREACHABLE();
|
_LIBCPP_UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
const_reference operator[](size_type) const {
|
const_reference operator[](size_type) const _NOEXCEPT {
|
||||||
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
|
_LIBCPP_ASSERT(false, "cannot call array<T, 0>::operator[] on a zero-sized array");
|
||||||
_LIBCPP_UNREACHABLE();
|
_LIBCPP_UNREACHABLE();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1330,9 +1330,9 @@ public:
|
|||||||
|
|
||||||
// element access:
|
// element access:
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
reference operator[](size_type __i);
|
reference operator[](size_type __i) _NOEXCEPT;
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
const_reference operator[](size_type __i) const;
|
const_reference operator[](size_type __i) const _NOEXCEPT;
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
reference at(size_type __i);
|
reference at(size_type __i);
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
@@ -1745,7 +1745,7 @@ deque<_Tp, _Allocator>::shrink_to_fit() _NOEXCEPT
|
|||||||
template <class _Tp, class _Allocator>
|
template <class _Tp, class _Allocator>
|
||||||
inline
|
inline
|
||||||
typename deque<_Tp, _Allocator>::reference
|
typename deque<_Tp, _Allocator>::reference
|
||||||
deque<_Tp, _Allocator>::operator[](size_type __i)
|
deque<_Tp, _Allocator>::operator[](size_type __i) _NOEXCEPT
|
||||||
{
|
{
|
||||||
size_type __p = __base::__start_ + __i;
|
size_type __p = __base::__start_ + __i;
|
||||||
return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
|
return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
|
||||||
@@ -1754,7 +1754,7 @@ deque<_Tp, _Allocator>::operator[](size_type __i)
|
|||||||
template <class _Tp, class _Allocator>
|
template <class _Tp, class _Allocator>
|
||||||
inline
|
inline
|
||||||
typename deque<_Tp, _Allocator>::const_reference
|
typename deque<_Tp, _Allocator>::const_reference
|
||||||
deque<_Tp, _Allocator>::operator[](size_type __i) const
|
deque<_Tp, _Allocator>::operator[](size_type __i) const _NOEXCEPT
|
||||||
{
|
{
|
||||||
size_type __p = __base::__start_ + __i;
|
size_type __p = __base::__start_ + __i;
|
||||||
return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
|
return *(*(__base::__map_.begin() + __p / __base::__block_size) + __p % __base::__block_size);
|
||||||
|
|||||||
@@ -12,6 +12,7 @@
|
|||||||
// const_reference operator[] (size_type); // constexpr in C++14
|
// const_reference operator[] (size_type); // constexpr in C++14
|
||||||
// reference at (size_type)
|
// reference at (size_type)
|
||||||
// const_reference at (size_type); // constexpr in C++14
|
// const_reference at (size_type); // constexpr in C++14
|
||||||
|
// Libc++ marks these as noexcept
|
||||||
|
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
@@ -36,6 +37,8 @@ int main(int, char**)
|
|||||||
typedef double T;
|
typedef double T;
|
||||||
typedef std::array<T, 3> C;
|
typedef std::array<T, 3> C;
|
||||||
C c = {1, 2, 3.5};
|
C c = {1, 2, 3.5};
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
|
||||||
C::reference r1 = c[0];
|
C::reference r1 = c[0];
|
||||||
assert(r1 == 1);
|
assert(r1 == 1);
|
||||||
r1 = 5.5;
|
r1 = 5.5;
|
||||||
@@ -50,6 +53,8 @@ int main(int, char**)
|
|||||||
typedef double T;
|
typedef double T;
|
||||||
typedef std::array<T, 3> C;
|
typedef std::array<T, 3> C;
|
||||||
const C c = {1, 2, 3.5};
|
const C c = {1, 2, 3.5};
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
|
||||||
C::const_reference r1 = c[0];
|
C::const_reference r1 = c[0];
|
||||||
assert(r1 == 1);
|
assert(r1 == 1);
|
||||||
C::const_reference r2 = c[2];
|
C::const_reference r2 = c[2];
|
||||||
@@ -59,6 +64,8 @@ int main(int, char**)
|
|||||||
typedef double T;
|
typedef double T;
|
||||||
typedef std::array<T, 0> C;
|
typedef std::array<T, 0> C;
|
||||||
C c = {};
|
C c = {};
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
|
||||||
C const& cc = c;
|
C const& cc = c;
|
||||||
static_assert((std::is_same<decltype(c[0]), T &>::value), "");
|
static_assert((std::is_same<decltype(c[0]), T &>::value), "");
|
||||||
static_assert((std::is_same<decltype(cc[0]), const T &>::value), "");
|
static_assert((std::is_same<decltype(cc[0]), const T &>::value), "");
|
||||||
@@ -73,6 +80,8 @@ int main(int, char**)
|
|||||||
typedef double T;
|
typedef double T;
|
||||||
typedef std::array<const T, 0> C;
|
typedef std::array<const T, 0> C;
|
||||||
C c = {{}};
|
C c = {{}};
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
|
||||||
C const& cc = c;
|
C const& cc = c;
|
||||||
static_assert((std::is_same<decltype(c[0]), const T &>::value), "");
|
static_assert((std::is_same<decltype(c[0]), const T &>::value), "");
|
||||||
static_assert((std::is_same<decltype(cc[0]), const T &>::value), "");
|
static_assert((std::is_same<decltype(cc[0]), const T &>::value), "");
|
||||||
@@ -88,6 +97,8 @@ int main(int, char**)
|
|||||||
typedef double T;
|
typedef double T;
|
||||||
typedef std::array<T, 3> C;
|
typedef std::array<T, 3> C;
|
||||||
constexpr C c = {1, 2, 3.5};
|
constexpr C c = {1, 2, 3.5};
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
|
||||||
|
|
||||||
constexpr T t1 = c[0];
|
constexpr T t1 = c[0];
|
||||||
static_assert (t1 == 1, "");
|
static_assert (t1 == 1, "");
|
||||||
|
|||||||
@@ -19,11 +19,13 @@
|
|||||||
//
|
//
|
||||||
// reference back();
|
// reference back();
|
||||||
// const_reference back() const;
|
// const_reference back() const;
|
||||||
|
// libc++ marks these as 'noexcept'
|
||||||
|
|
||||||
#include <deque>
|
#include <deque>
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
#include "min_allocator.h"
|
#include "min_allocator.h"
|
||||||
|
#include "test_macros.h"
|
||||||
|
|
||||||
template <class C>
|
template <class C>
|
||||||
C
|
C
|
||||||
@@ -50,7 +52,10 @@ make(int size, int start = 0 )
|
|||||||
int main(int, char**)
|
int main(int, char**)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::deque<int> c = make<std::deque<int> >(10);
|
typedef std::deque<int> C;
|
||||||
|
C c = make<std::deque<int> >(10);
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
assert(c[i] == i);
|
assert(c[i] == i);
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
@@ -59,7 +64,10 @@ int main(int, char**)
|
|||||||
assert(c.back() == 9);
|
assert(c.back() == 9);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
const std::deque<int> c = make<std::deque<int> >(10);
|
typedef std::deque<int> C;
|
||||||
|
const C c = make<std::deque<int> >(10);
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
assert(c[i] == i);
|
assert(c[i] == i);
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
@@ -69,7 +77,10 @@ int main(int, char**)
|
|||||||
}
|
}
|
||||||
#if TEST_STD_VER >= 11
|
#if TEST_STD_VER >= 11
|
||||||
{
|
{
|
||||||
std::deque<int, min_allocator<int>> c = make<std::deque<int, min_allocator<int>> >(10);
|
typedef std::deque<int, min_allocator<int>> C;
|
||||||
|
C c = make<std::deque<int, min_allocator<int>> >(10);
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::reference, decltype(c[0]));
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
assert(c[i] == i);
|
assert(c[i] == i);
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
@@ -78,7 +89,10 @@ int main(int, char**)
|
|||||||
assert(c.back() == 9);
|
assert(c.back() == 9);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
const std::deque<int, min_allocator<int>> c = make<std::deque<int, min_allocator<int>> >(10);
|
typedef std::deque<int, min_allocator<int>> C;
|
||||||
|
const C c = make<std::deque<int, min_allocator<int>> >(10);
|
||||||
|
LIBCPP_ASSERT_NOEXCEPT(c[0]);
|
||||||
|
ASSERT_SAME_TYPE(C::const_reference, decltype(c[0]));
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
assert(c[i] == i);
|
assert(c[i] == i);
|
||||||
for (int i = 0; i < 10; ++i)
|
for (int i = 0; i < 10; ++i)
|
||||||
|
|||||||
Reference in New Issue
Block a user