diff mbox

Fix std::experimental::shared_ptr SFINAE constraints

Message ID 20161019115720.GY2922@redhat.com
State New
Headers show

Commit Message

Jonathan Wakely Oct. 19, 2016, 11:57 a.m. UTC
On 19/10/16 10:35 +0100, Jonathan Wakely wrote:
>There are lots of SFINAE checks missing from experimental::shared_ptr,

>and we even test that shared_ptr<base[]>(new derived[1]) works, when

>it should be ill-formed.


Moar tests!

Tested powerpc64le-linux, committed to trunk.
diff mbox

Patch

commit 9258e632b7810db67cef4e60c5c22bef3571b3bb
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Oct 19 12:06:31 2016 +0100

    Test experimental::shared_ptr construction from other smart pointers
    
    	* testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc:
    	Add tests for valid and invalid conversions.
    	* testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc:
    	Likewise.

diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc
index 631212f..0e61a3c 100644
--- a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc
+++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/unique_ptr_ctor.cc
@@ -29,10 +29,35 @@  struct A : std::experimental::enable_shared_from_this<A>
   ~A() { ++destroyed; }
 };
 
+struct B : A { };
+
 // 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const]
 
 // Construction from unique_ptr<A[]>
 
+template<typename From, typename To>
+constexpr bool constructible()
+{
+  using std::experimental::shared_ptr;
+  using std::experimental::is_constructible_v;
+  using std::unique_ptr;
+  return is_constructible_v<shared_ptr<To>, unique_ptr<From>>;
+}
+
+static_assert(  constructible< A,    A    >(), "A -> A compatible" );
+static_assert( !constructible< A,    A[]  >(), "A -> A[] not compatible" );
+static_assert( !constructible< A,    A[1] >(), "A -> A[1] not compatible" );
+static_assert( !constructible< A[],  A    >(), "A[] -> A not compatible" );
+static_assert(  constructible< A[],  A[]  >(), "A[] -> A[] compatible" );
+static_assert( !constructible< A[],  A[1] >(), "A[] -> A[1] not compatible" );
+
+static_assert(  constructible< B,    A    >(), "B -> A compatible" );
+static_assert( !constructible< B,    A[]  >(), "B -> A[] not compatible" );
+static_assert( !constructible< B,    A[1] >(), "B -> A[1] not compatible" );
+static_assert( !constructible< B[],  A    >(), "B[] -> A not compatible" );
+static_assert( !constructible< B[],  A[]  >(), "B[] -> A[] not compatible" );
+static_assert( !constructible< B[],  A[1] >(), "B[2] -> A[1] not compatible" );
+
 void
 test01()
 {
diff --git a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc
index 71cf583..3436c01 100644
--- a/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc
+++ b/libstdc++-v3/testsuite/experimental/memory/shared_ptr/cons/weak_ptr_ctor.cc
@@ -23,11 +23,46 @@ 
 #include <testsuite_hooks.h>
 
 struct A { };
+struct B : A { };
 
 // 8.2.1.1 shared_ptr constructors [memory.smartptr.shared.const]
 
+template<typename From, typename To>
+constexpr bool constructible()
+{
+  using std::experimental::shared_ptr;
+  using std::experimental::weak_ptr;
+  using std::experimental::is_constructible_v;
+  return is_constructible_v<shared_ptr<To>, weak_ptr<From>>
+    && is_constructible_v<shared_ptr<To>, const weak_ptr<From>&>;
+}
+
+static_assert(  constructible< A,    A    >(), "A -> A compatible" );
+static_assert( !constructible< A,    A[]  >(), "A -> A[] not compatible" );
+static_assert( !constructible< A,    A[1] >(), "A -> A[1] not compatible" );
+static_assert( !constructible< A[],  A    >(), "A[] -> A not compatible" );
+static_assert(  constructible< A[],  A[]  >(), "A[] -> A[] compatible" );
+static_assert( !constructible< A[],  A[1] >(), "A[] -> A[1] not compatible" );
+static_assert( !constructible< A[1], A    >(), "A[1] -> A not compatible" );
+static_assert(  constructible< A[1], A[]  >(), "A[1] -> A[] compatible" );
+static_assert(  constructible< A[1], A[1] >(), "A[1] -> A[1] compatible" );
+static_assert( !constructible< A[2], A[1] >(), "A[2] -> A[1] not compatible" );
+
+static_assert(  constructible< B,    A    >(), "B -> A compatible" );
+static_assert( !constructible< B,    A[]  >(), "B -> A[] not compatible" );
+static_assert( !constructible< B,    A[1] >(), "B -> A[1] not compatible" );
+static_assert( !constructible< B[],  A    >(), "B[] -> A not compatible" );
+static_assert( !constructible< B[],  A[]  >(), "B[] -> A[] not compatible" );
+static_assert( !constructible< B[],  A[1] >(), "B[] -> A[1] not compatible" );
+static_assert( !constructible< B[1], A    >(), "B[] -> A not compatible" );
+static_assert( !constructible< B[1], A[]  >(), "B[] -> A[] not compatible" );
+static_assert( !constructible< B[1], A[1] >(), "B[] -> A[1] not compatible" );
+static_assert( !constructible< B[2], A[1] >(), "B[2] -> A[1] not compatible" );
+
+
+
 // Construction from weak_ptr
-int
+void
 test01()
 {
   A * a = new A[5];
@@ -39,14 +74,10 @@  test01()
   VERIFY( a3.get() == a );
   VERIFY( a2.use_count() == wa.use_count() );
   VERIFY( a3.use_count() == wa.use_count() );
-
-  return 0;
 }
 
-
 int
 main()
 {
   test01();
-  return 0;
 }