Fix incorrectly qualified return type from unique_ptr::get_deleter().

For reference deleter types the const qualifier on the return type
of get_deleter() should be ignored, and a non-const deleter should
be returned.

This patch fixes a bug where "const deleter_type&" is incorrectly
formed.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@300121 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2017-04-12 22:43:49 +00:00
parent 6d5fdc110f
commit 39683f1377
4 changed files with 73 additions and 31 deletions

View File

@@ -2110,8 +2110,8 @@ public:
typedef typename remove_reference<_T1>::type& _T1_reference; typedef typename remove_reference<_T1>::type& _T1_reference;
typedef typename remove_reference<_T2>::type& _T2_reference; typedef typename remove_reference<_T2>::type& _T2_reference;
typedef const typename remove_reference<_T1>::type& _T1_const_reference; typedef typename remove_reference<typename add_const<_T1>::type>::type& _T1_const_reference;
typedef const typename remove_reference<_T2>::type& _T2_const_reference; typedef typename remove_reference<typename add_const<_T2>::type>::type& _T2_const_reference;
_LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {} _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_(), __second_() {}
_LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1) _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
@@ -2166,7 +2166,8 @@ public:
typedef typename remove_reference<_T2>::type& _T2_reference; typedef typename remove_reference<_T2>::type& _T2_reference;
typedef const _T1& _T1_const_reference; typedef const _T1& _T1_const_reference;
typedef const typename remove_reference<_T2>::type& _T2_const_reference; typedef typename remove_reference<typename add_const<_T2>::type>::type&
_T2_const_reference;
_LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {} _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __second_() {}
_LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1) _LIBCPP_INLINE_VISIBILITY explicit __libcpp_compressed_pair_imp(_T1_param __t1)
@@ -2219,7 +2220,8 @@ public:
typedef typename remove_reference<_T1>::type& _T1_reference; typedef typename remove_reference<_T1>::type& _T1_reference;
typedef _T2& _T2_reference; typedef _T2& _T2_reference;
typedef const typename remove_reference<_T1>::type& _T1_const_reference; typedef typename remove_reference<typename add_const<_T1>::type>::type&
_T1_const_reference;
typedef const _T2& _T2_const_reference; typedef const _T2& _T2_const_reference;
_LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {} _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() : __first_() {}
@@ -2460,7 +2462,8 @@ private:
struct __nat {int __for_bool_;}; struct __nat {int __for_bool_;};
typedef typename remove_reference<deleter_type>::type& _Dp_reference; typedef typename remove_reference<deleter_type>::type& _Dp_reference;
typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference; typedef typename remove_reference<typename add_const<deleter_type>::type>::type&
_Dp_const_reference;
public: public:
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
: __ptr_(pointer()) : __ptr_(pointer())
@@ -2649,7 +2652,8 @@ private:
struct __nat {int __for_bool_;}; struct __nat {int __for_bool_;};
typedef typename remove_reference<deleter_type>::type& _Dp_reference; typedef typename remove_reference<deleter_type>::type& _Dp_reference;
typedef const typename remove_reference<deleter_type>::type& _Dp_const_reference; typedef typename remove_reference<typename add_const<deleter_type>::type>::type&
_Dp_const_reference;
public: public:
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR unique_ptr() _NOEXCEPT
: __ptr_(pointer()) : __ptr_(pointer())

View File

@@ -15,17 +15,16 @@
#include <memory> #include <memory>
#include <cassert> #include <cassert>
#include "test_macros.h"
struct Deleter struct Deleter {
{ void operator()(void*) const {}
void operator()(void*) {}
int test() {return 5;} int test() { return 5; }
int test() const {return 6;} int test() const { return 6; }
}; };
int main() int main() {
{
{ {
std::unique_ptr<int[], Deleter> p; std::unique_ptr<int[], Deleter> p;
assert(p.get_deleter().test() == 5); assert(p.get_deleter().test() == 5);
@@ -34,4 +33,24 @@ int main()
const std::unique_ptr<int[], Deleter> p; const std::unique_ptr<int[], Deleter> p;
assert(p.get_deleter().test() == 6); assert(p.get_deleter().test() == 6);
} }
{
typedef std::unique_ptr<int[], const Deleter&> UPtr;
const Deleter d;
UPtr p(nullptr, d);
const UPtr& cp = p;
ASSERT_SAME_TYPE(decltype(p.get_deleter()), const Deleter&);
ASSERT_SAME_TYPE(decltype(cp.get_deleter()), const Deleter&);
assert(p.get_deleter().test() == 6);
assert(cp.get_deleter().test() == 6);
}
{
typedef std::unique_ptr<int[], Deleter&> UPtr;
Deleter d;
UPtr p(nullptr, d);
const UPtr& cp = p;
ASSERT_SAME_TYPE(decltype(p.get_deleter()), Deleter&);
ASSERT_SAME_TYPE(decltype(cp.get_deleter()), Deleter&);
assert(p.get_deleter().test() == 5);
assert(cp.get_deleter().test() == 5);
}
} }

View File

@@ -15,17 +15,16 @@
#include <memory> #include <memory>
#include <cassert> #include <cassert>
#include "test_macros.h"
struct Deleter struct Deleter {
{ void operator()(void*) const {}
void operator()(void*) {}
int test() {return 5;} int test() { return 5; }
int test() const {return 6;} int test() const { return 6; }
}; };
int main() int main() {
{
{ {
std::unique_ptr<int, Deleter> p; std::unique_ptr<int, Deleter> p;
assert(p.get_deleter().test() == 5); assert(p.get_deleter().test() == 5);
@@ -34,4 +33,24 @@ int main()
const std::unique_ptr<int, Deleter> p; const std::unique_ptr<int, Deleter> p;
assert(p.get_deleter().test() == 6); assert(p.get_deleter().test() == 6);
} }
{
typedef std::unique_ptr<int, const Deleter&> UPtr;
const Deleter d;
UPtr p(nullptr, d);
const UPtr& cp = p;
ASSERT_SAME_TYPE(decltype(p.get_deleter()), const Deleter&);
ASSERT_SAME_TYPE(decltype(cp.get_deleter()), const Deleter&);
assert(p.get_deleter().test() == 6);
assert(cp.get_deleter().test() == 6);
}
{
typedef std::unique_ptr<int, Deleter&> UPtr;
Deleter d;
UPtr p(nullptr, d);
const UPtr& cp = p;
ASSERT_SAME_TYPE(decltype(p.get_deleter()), Deleter&);
ASSERT_SAME_TYPE(decltype(cp.get_deleter()), Deleter&);
assert(p.get_deleter().test() == 5);
assert(cp.get_deleter().test() == 5);
}
} }

View File

@@ -184,7 +184,7 @@ struct is_same<T, T> { enum {value = 1}; };
} // namespace test_macros_detail } // namespace test_macros_detail
#define ASSERT_SAME_TYPE(...) \ #define ASSERT_SAME_TYPE(...) \
static_assert(test_macros_detail::is_same<__VA_ARGS__>::value, \ static_assert((test_macros_detail::is_same<__VA_ARGS__>::value), \
"Types differ uexpectedly") "Types differ uexpectedly")
#ifndef TEST_HAS_NO_EXCEPTIONS #ifndef TEST_HAS_NO_EXCEPTIONS