This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
[patch] Relax Debug Mode assertions on operator-> for smart pointers.
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: libstdc++ at gcc dot gnu dot org, gcc-patches at gcc dot gnu dot org
- Date: Mon, 7 Sep 2015 18:17:21 +0100
- Subject: [patch] Relax Debug Mode assertions on operator-> for smart pointers.
- Authentication-results: sourceware.org; auth=none
The standard says that unique_ptr::operator->() and
shared_ptr::operator->() have preconditions that the pointer is not
null, but that's not strictly necessary and prevents using that
function to get a raw pointer from a smart pointer in generic code.
This changes the _GLIBCXX_DEBUG_ASSERT to _GLIBCXX_DEBUG_PEDASSERT so
they only fire in pedantic debug mode.
Tested powerpc64le-linux, committed to trunk.
commit b81acb46e3634cb827f45988c90148612925664f
Author: Jonathan Wakely <jwakely@redhat.com>
Date: Mon Sep 7 17:47:25 2015 +0100
Relax Debug Mode assertions on operator-> for smart pointers.
* include/bits/shared_ptr_base.h (__shared_ptr::operator->): Change
_GLIBCXX_DEBUG_ASSERT to _GLIBCXX_DEBUG_PEDASSERT.
* include/bits/unique_ptr.h (unique_ptr::operator->): Likewise.
* testsuite/20_util/shared_ptr/observers/get.cc: Test operator-> on
empty shared_ptr.
diff --git a/libstdc++-v3/include/bits/shared_ptr_base.h b/libstdc++-v3/include/bits/shared_ptr_base.h
index f2f577b..75f1a0d 100644
--- a/libstdc++-v3/include/bits/shared_ptr_base.h
+++ b/libstdc++-v3/include/bits/shared_ptr_base.h
@@ -1054,7 +1054,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
_Tp*
operator->() const noexcept
{
- _GLIBCXX_DEBUG_ASSERT(_M_ptr != 0);
+ _GLIBCXX_DEBUG_PEDASSERT(_M_ptr != 0);
return _M_ptr;
}
diff --git a/libstdc++-v3/include/bits/unique_ptr.h b/libstdc++-v3/include/bits/unique_ptr.h
index 8ab55da..bb96951 100644
--- a/libstdc++-v3/include/bits/unique_ptr.h
+++ b/libstdc++-v3/include/bits/unique_ptr.h
@@ -295,7 +295,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
pointer
operator->() const noexcept
{
- _GLIBCXX_DEBUG_ASSERT(get() != pointer());
+ _GLIBCXX_DEBUG_PEDASSERT(get() != pointer());
return get();
}
diff --git a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc
index 85fc71d..867f07a 100644
--- a/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc
+++ b/libstdc++-v3/testsuite/20_util/shared_ptr/observers/get.cc
@@ -63,11 +63,24 @@ test03()
VERIFY( &p->i == &a->i );
}
+void
+test04()
+{
+ bool test __attribute__((unused)) = true;
+
+#if !(defined _GLIBCXX_DEBUG && defined _GLIBCXX_DEBUG_PEDANTIC)
+ std::shared_ptr<int> p;
+ auto np = p.operator->();
+ VERIFY( np == nullptr );
+#endif
+}
+
int
main()
{
test01();
test02();
test03();
+ test04();
return 0;
}