This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [v3 PATCH] Implement 2801, Default-constructibility of unique_ptr.
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: Ville Voutilainen <ville dot voutilainen at gmail dot com>
- Cc: libstdc++ <libstdc++ at gcc dot gnu dot org>, "gcc-patches at gcc dot gnu dot org" <gcc-patches at gcc dot gnu dot org>
- Date: Thu, 22 Dec 2016 17:11:23 +0000
- Subject: Re: [v3 PATCH] Implement 2801, Default-constructibility of unique_ptr.
- Authentication-results: sourceware.org; auth=none
- References: <CAFk2RUbSRwUJP0F=XPfeCmefY-cHrV3LwNvzW_XLNR_R3-r+0Q@mail.gmail.com>
On 20/12/16 22:52 +0200, Ville Voutilainen wrote:
diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h
index 56e6ec0..63dff37 100644
--- a/libstdc++-v3/include/bits/unique_ptr.h
+++ b/libstdc++-v3/include/bits/unique_ptr.h
@@ -175,10 +175,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Constructors.
/// Default constructor, creates a unique_ptr that owns nothing.
+ template <typename _Up = deleter_type,
+ typename enable_if<
+ __and_<__not_<is_pointer<_Up>>,
+ is_default_constructible<_Up>>::value,
+ bool>::type = false>
Instead of repeating this condition half a dozen times, we could put
it in the __uniq_ptr_impl class template and reuse it, as in the
attached patch (and similarly for the unique_ptr<t[]> specialization).
What do you think?
constexpr unique_ptr() noexcept
: _M_t()
- { static_assert(!is_pointer<deleter_type>::value,
- "constructed with null function pointer deleter"); }
+ { }
The bodies of these constructors should be indented now that they're
templates.
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/unique_ptr/cons/default.cc
@@ -0,0 +1,40 @@
+// { dg-do compile { target c++11 } }
+
+// Copyright (C) 2011-2016 Free Software Foundation, Inc.
Is this substantially copied from an existing file, or should it just
be 2016? (Not that it really matters, as I don't think we should have
copyright notices on tests like this at all, but that's something to
worry about another time.
diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h
index 56e6ec0..f9ab9a6 100644
--- a/libstdc++-v3/include/bits/unique_ptr.h
+++ b/libstdc++-v3/include/bits/unique_ptr.h
@@ -130,6 +130,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
};
public:
+ using _DeleterConstraint = enable_if<
+ __and_<__not_<is_pointer<_Dp>, is_default_constructible<_Dp>>>::value>;
+
using pointer = typename _Ptr<_Tp, _Dp>::type;
__uniq_ptr_impl() = default;
@@ -152,6 +155,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template <typename _Tp, typename _Dp = default_delete<_Tp>>
class unique_ptr
{
+ using _DeleterConstraint = __uniq_ptr_impl<_Tp, _Dp>::_DeleterConstraint;
+
__uniq_ptr_impl<_Tp, _Dp> _M_t;
public:
@@ -175,10 +180,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// Constructors.
/// Default constructor, creates a unique_ptr that owns nothing.
- constexpr unique_ptr() noexcept
- : _M_t()
- { static_assert(!is_pointer<deleter_type>::value,
- "constructed with null function pointer deleter"); }
+ template<typename = typename _DeleterConstraint::type>
+ constexpr unique_ptr() noexcept
+ : _M_t()
+ { }
/** Takes ownership of a pointer.
*
@@ -186,11 +191,11 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
*
* The deleter will be value-initialized.
*/
- explicit
- unique_ptr(pointer __p) noexcept
- : _M_t(__p)
- { static_assert(!is_pointer<deleter_type>::value,
- "constructed with null function pointer deleter"); }
+ template<typename = typename _DeleterConstraint::type>
+ explicit
+ unique_ptr(pointer __p) noexcept
+ : _M_t(__p)
+ { }
/** Takes ownership of a pointer.
*
@@ -218,7 +223,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
"rvalue deleter bound to reference"); }
/// Creates a unique_ptr that owns nothing.
- constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
+ template<typename = typename _DeleterConstraint::type>
+ constexpr unique_ptr(nullptr_t) noexcept : unique_ptr() { }
// Move constructors.