#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);
/// 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();
/// 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];
--- /dev/null
+// { 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>&&>()) );
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
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[]
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[]
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
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( p.get() == a );
+ static_assert( noexcept(p.get()), "non-throwing" );
}
// operator*
A * const a = new A;
const std::shared_ptr<A> p(a);
VERIFY( &*p == a );
+ static_assert( noexcept(*p), "non-throwing" );
}
// operator->
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
#endif
}
-int
+int
main()
{
test01();
--- /dev/null
+// { 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]), "" );