From 78a8b676f1bd4fb799f3696486f45adde11a32cb Mon Sep 17 00:00:00 2001 From: Jonathan Wakely Date: Fri, 16 Jun 2017 16:20:14 +0100 Subject: [PATCH] Add std::get_deleter overload with correct signature * include/bits/shared_ptr.h (get_deleter): Add overload matching standard signature. * include/bits/shared_ptr_base.h (__shared_ptr): Declare new get_deleter overload as a friend. * testsuite/20_util/shared_ptr/misc/get_deleter.cc: New. From-SVN: r249267 --- libstdc++-v3/ChangeLog | 8 ++++ libstdc++-v3/include/bits/shared_ptr.h | 12 ++++- libstdc++-v3/include/bits/shared_ptr_base.h | 3 ++ .../20_util/shared_ptr/misc/get_deleter.cc | 44 +++++++++++++++++++ 4 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 libstdc++-v3/testsuite/20_util/shared_ptr/misc/get_deleter.cc diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index f8a6fc8987c..8e1ba2d47c2 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,11 @@ +2017-06-16 Jonathan Wakely + + * include/bits/shared_ptr.h (get_deleter): Add overload matching + standard signature. + * include/bits/shared_ptr_base.h (__shared_ptr): Declare new + get_deleter overload as a friend. + * testsuite/20_util/shared_ptr/misc/get_deleter.cc: New. + 2017-06-16 Jakub Jelinek PR libstdc++/81092 diff --git a/libstdc++-v3/include/bits/shared_ptr.h b/libstdc++-v3/include/bits/shared_ptr.h index 264e35ce2d0..999a034dc9b 100644 --- a/libstdc++-v3/include/bits/shared_ptr.h +++ b/libstdc++-v3/include/bits/shared_ptr.h @@ -70,7 +70,6 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return __os; } - /// 20.7.2.2.10 shared_ptr get_deleter template inline _Del* get_deleter(const __shared_ptr<_Tp, _Lp>& __p) noexcept @@ -82,6 +81,17 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION #endif } + /// 20.7.2.2.10 shared_ptr get_deleter + template + inline _Del* + get_deleter(const shared_ptr<_Tp>& __p) noexcept + { +#if __cpp_rtti + return static_cast<_Del*>(__p._M_get_deleter(typeid(_Del))); +#else + return 0; +#endif + } /** * @brief A smart pointer with reference-counted copy semantics. diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h index a07058c59c4..7e6766ba7d5 100644 --- a/libstdc++-v3/include/bits/shared_ptr_base.h +++ b/libstdc++-v3/include/bits/shared_ptr_base.h @@ -1402,6 +1402,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION template friend _Del* get_deleter(const __shared_ptr<_Tp1, _Lp1>&) noexcept; + template + friend _Del* get_deleter(const shared_ptr<_Tp1>&) noexcept; + element_type* _M_ptr; // Contained pointer. __shared_count<_Lp> _M_refcount; // Reference counter. }; diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/misc/get_deleter.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/misc/get_deleter.cc new file mode 100644 index 00000000000..cb5f4c773ad --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/shared_ptr/misc/get_deleter.cc @@ -0,0 +1,44 @@ +// Copyright (C) 2017 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// { dg-do run { target c++11 } } + +#include +#include + +struct Del { + template void operator()(T* p) const noexcept { delete p; } +}; + +Del* (*f1)(const std::shared_ptr&) = std::get_deleter; + +void +test01() +{ + std::shared_ptr p; + VERIFY( std::get_deleter(p) == nullptr ); + p = std::shared_ptr(new int, Del()); + VERIFY( std::get_deleter(p) != nullptr ); + p = std::shared_ptr(new int); + VERIFY( std::get_deleter(p) == nullptr ); +} + +int +main() +{ + test01(); +} -- 2.30.2