aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJonathan Wakely <jwakely@redhat.com>2021-06-24 12:56:20 +0100
committerJonathan Wakely <jwakely@redhat.com>2021-06-24 14:04:02 +0100
commit17bc3848e065c0980523e1a1592f2f03b24b4f1c (patch)
tree0149b4f6b118bc81b9423c4f634c74b678f7d430
parenta21dc9d1529b8a8071e36b22b6e8492fc2ce7d5a (diff)
downloadgcc-17bc3848e065c0980523e1a1592f2f03b24b4f1c.zip
gcc-17bc3848e065c0980523e1a1592f2f03b24b4f1c.tar.gz
gcc-17bc3848e065c0980523e1a1592f2f03b24b4f1c.tar.bz2
libstdc++: Implement LWG 2762 for std::unique_ptr::operator*
The LWG issue proposes to add a conditional noexcept-specifier to std::unique_ptr's dereference operator. The issue is currently in Tentatively Ready status, but even if it isn't voted into the draft, we can do it as a conforming extensions. This commit also adds a similar noexcept-specifier to operator[] for the unique_ptr<T[], D> partial specialization. Also ensure that all dereference operators for shared_ptr are noexcept, and adds tests for the std::optional accessors modified by the issue, which were already noexcept in our implementation. Signed-off-by: Jonathan Wakely <jwakely@redhat.com> libstdc++-v3/ChangeLog: * include/bits/shared_ptr_base.h (__shared_ptr_access::operator[]): Add noexcept. * include/bits/unique_ptr.h (unique_ptr::operator*): Add conditional noexcept as per LWG 2762. * testsuite/20_util/shared_ptr/observers/array.cc: Check that dereferencing cannot throw. * testsuite/20_util/shared_ptr/observers/get.cc: Likewise. * testsuite/20_util/optional/observers/lwg2762.cc: New test. * testsuite/20_util/unique_ptr/lwg2762.cc: New test.
-rw-r--r--libstdc++-v3/include/bits/shared_ptr_base.h2
-rw-r--r--libstdc++-v3/include/bits/unique_ptr.h3
-rw-r--r--libstdc++-v3/testsuite/20_util/optional/observers/lwg2762.cc21
-rw-r--r--libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc4
-rw-r--r--libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc5
-rw-r--r--libstdc++-v3/testsuite/20_util/unique_ptr/lwg2762.cc43
6 files changed, 75 insertions, 3 deletions
diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index eb9ad23..5be935d 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -1035,7 +1035,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
#endif
element_type&
- operator[](ptrdiff_t __i) const
+ operator[](ptrdiff_t __i) const noexcept
{
__glibcxx_assert(_M_get() != nullptr);
__glibcxx_assert(!extent<_Tp>::value || __i < extent<_Tp>::value);
diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h
index 6e55375..1781fe1 100644
--- a/libstdc++-v3/include/bits/unique_ptr.h
+++ b/libstdc++-v3/include/bits/unique_ptr.h
@@ -402,7 +402,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Dereference the stored pointer.
typename add_lvalue_reference<element_type>::type
- operator*() const
+ operator*() const noexcept(noexcept(*std::declval<pointer>()))
{
__glibcxx_assert(get() != pointer());
return *get();
@@ -655,6 +655,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
/// Access an element of owned array.
typename std::add_lvalue_reference<element_type>::type
operator[](size_t __i) const
+ noexcept(noexcept(std::declval<pointer>()[std::declval<size_t&>()]))
{
__glibcxx_assert(get() != pointer());
return get()[__i];
diff --git a/libstdc++-v3/testsuite/20_util/optional/observers/lwg2762.cc b/libstdc++-v3/testsuite/20_util/optional/observers/lwg2762.cc
new file mode 100644
index 0000000..a0cf0bc
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/optional/observers/lwg2762.cc
@@ -0,0 +1,21 @@
+// { dg-do compile { target c++17 } }
+
+// LWG 2762 adds noexcept to operator-> and operator*
+#include <optional>
+
+struct S
+{
+ void can_throw();
+ void cannot_throw() noexcept;
+};
+
+static_assert( ! noexcept(std::declval<std::optional<S>&>()->can_throw()) );
+static_assert( noexcept(std::declval<std::optional<S>&>()->cannot_throw()) );
+
+static_assert( noexcept(std::declval<std::optional<S>&>().operator->()) );
+static_assert( noexcept(std::declval<std::optional<int>&>().operator->()) );
+
+static_assert( noexcept(*std::declval<std::optional<int>&>()) );
+static_assert( noexcept(*std::declval<const std::optional<int>&>()) );
+static_assert( noexcept(*std::declval<std::optional<int>&&>()) );
+static_assert( noexcept(*std::declval<const std::optional<int>&&>()) );
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc
index f6acb1f..7fd6c01 100644
--- a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc
+++ b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/array.cc
@@ -34,6 +34,7 @@ test01()
A * const a = new A[2];
const std::shared_ptr<A[2]> p(a);
VERIFY( p.get() == a );
+ static_assert( noexcept(p.get()), "non-throwing" );
}
// get
@@ -43,6 +44,7 @@ test02()
A * const a = new A[2];
const std::shared_ptr<A[]> p(a);
VERIFY( p.get() == a );
+ static_assert( noexcept(p.get()), "non-throwing" );
}
// operator[]
@@ -52,6 +54,7 @@ test03()
A * const a = new A[2];
const std::shared_ptr<A[2]> p(a);
VERIFY( &p[0] == a );
+ static_assert( noexcept(p[0]), "non-throwing" );
}
// operator[]
@@ -61,6 +64,7 @@ test04()
A * const a = new A[2];
const std::shared_ptr<A[]> p(a);
VERIFY( &p[0] == a );
+ static_assert( noexcept(p[0]), "non-throwing" );
}
int
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc
index cd1282b..6f2cb9f 100644
--- a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc
+++ b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc
@@ -37,6 +37,7 @@ test01()
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( p.get() == a );
+ static_assert( noexcept(p.get()), "non-throwing" );
}
// operator*
@@ -46,6 +47,7 @@ test02()
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( &*p == a );
+ static_assert( noexcept(*p), "non-throwing" );
}
// operator->
@@ -55,6 +57,7 @@ test03()
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( &p->i == &a->i );
+ static_assert( noexcept(p->i), "non-throwing" );
}
void
@@ -67,7 +70,7 @@ test04()
#endif
}
-int
+int
main()
{
test01();
diff --git a/libstdc++-v3/testsuite/20_util/unique_ptr/lwg2762.cc b/libstdc++-v3/testsuite/20_util/unique_ptr/lwg2762.cc
new file mode 100644
index 0000000..3cc2ea6
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/lwg2762.cc
@@ -0,0 +1,43 @@
+// { dg-do compile { target c++11 } }
+#include <memory>
+
+// 2762. unique_ptr operator*() should be noexcept
+static_assert( noexcept(*std::declval<std::unique_ptr<long>>()), "LWG 2762" );
+
+template<bool B>
+struct deleter
+{
+ struct pointer
+ {
+ int& operator*() && noexcept(B); // this is used by unique_ptr
+ int& operator*() const& = delete; // this should not be
+
+ int& operator[](std::size_t) && noexcept(B); // this is used by unique_ptr
+ int& operator[](std::size_t) const& = delete; // should not be used
+ int& operator[](int) && = delete; // should not be used
+ int& operator[](double) && = delete; // should not be used
+
+ int* operator->() noexcept(false); // noexcept here doesn't affect anything
+
+ // Needed for NullablePointer requirements
+ pointer(int* = nullptr);
+ bool operator==(const pointer&) const noexcept;
+ bool operator!=(const pointer&) const noexcept;
+ };
+
+ void operator()(pointer) const noexcept { }
+};
+
+template<typename T, bool Nothrow>
+ using UPtr = std::unique_ptr<T, deleter<Nothrow>>;
+
+// noexcept-specifier depends on the pointer type
+static_assert( noexcept(*std::declval<UPtr<int, true>&>()), "" );
+static_assert( ! noexcept(*std::declval<UPtr<int, false>&>()), "" );
+
+// This has always been required, even in C++11.
+static_assert( noexcept(std::declval<UPtr<int, false>&>().operator->()), "" );
+
+// This is not required by the standard
+static_assert( noexcept(std::declval<UPtr<int[], true>&>()[0]), "" );
+static_assert( ! noexcept(std::declval<UPtr<int[], false>&>()[0]), "" );