= __and_< is_base_of<_Tp, _Up>,
__not_<is_same<__remove_cv<_Tp>, __remove_cv<_Up>>> >;
+ // This checks whether p[n] is noexcept, but fails gracefully when
+ // element_type is incomplete. The standard requires a complete type
+ // for unique_ptr<T[], D>, but we try to support it anyway (PR 101236).
+ template<typename _Ptr, typename _Elt>
+ static constexpr auto
+ _S_nothrow_deref(size_t __n)
+ -> decltype(sizeof(_Elt) != 0) // PR c++/101239
+ { return noexcept(std::declval<_Ptr>()[__n]); }
+
+ template<typename _Ptr, typename _Elt>
+ static constexpr bool
+ _S_nothrow_deref(...)
+ { return false; }
+
public:
using pointer = typename __uniq_ptr_impl<_Tp, _Dp>::pointer;
using element_type = _Tp;
/// 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&>()]))
+ noexcept(_S_nothrow_deref<pointer, element_type>(0))
{
__glibcxx_assert(get() != pointer());
return get()[__i];
void f(void** p)
{
::new (p[0]) std::unique_ptr<Incomplete>();
- ::new (p[1]) std::unique_ptr<Incomplete[]>();
// PR libstdc++/87704
- ::new (p[2]) std::unique_ptr<Incomplete>(nullptr);
- ::new (p[3]) std::unique_ptr<Incomplete[]>(nullptr);
+ ::new (p[1]) std::unique_ptr<Incomplete>(nullptr);
+}
+
+// The standard says "T shall be a complete type" for unique_ptr<T[], D>
+// so this is a GCC extension.
+void f_array(void** p)
+{
+ ::new (p[0]) std::unique_ptr<Incomplete[]>();
+
+ // PR libstdc++/87704
+ ::new (p[1]) std::unique_ptr<Incomplete[]>(nullptr);
}
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>&>()), "" );
+static_assert( noexcept(*std::declval<UPtr<int, true>&>()), "LWG 2762" );
+static_assert( ! noexcept(*std::declval<UPtr<int, false>&>()), "LWG 2762" );
// 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]), "" );
+static_assert( noexcept(std::declval<std::unique_ptr<long>>().operator->()),
+ "operator-> is always noexcept" );
+static_assert( noexcept(std::declval<UPtr<int, false>&>().operator->()),
+ "operator-> is always noexcept" );
+
+// This is not required by the standard, but we make it depend on the pointer.
+static_assert( noexcept(std::declval<std::unique_ptr<long[]>>()[0]), "QoI" );
+static_assert( noexcept(std::declval<UPtr<int[], true>&>()[0]), "QoI" );
+static_assert( ! noexcept(std::declval<UPtr<int[], false>&>()[0]), "QoI" );
+
+// This is forbidden by the standard ("T shall be a complete type")
+// but we try to support it anyway, see PR libstdc++/101236.
+struct Incomplete;
+static_assert( ! noexcept(std::declval<UPtr<Incomplete[], true>>()[0]),
+ "this would be noexcept if the type was complete");
+static_assert( ! noexcept(std::declval<UPtr<Incomplete[], false>>()[0]),
+ "this would still be noexcept(false) if the type was complete");