Home | History | Annotate | Download | only in unique.ptr.ctor
      1 //===----------------------------------------------------------------------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is dual licensed under the MIT and the University of Illinois Open
      6 // Source Licenses. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 
     10 // UNSUPPORTED: c++98, c++03
     11 
     12 // <memory>
     13 
     14 // unique_ptr
     15 
     16 // Test unique_ptr converting move ctor
     17 
     18 #include <memory>
     19 #include <cassert>
     20 
     21 #include "deleter_types.h"
     22 #include "unique_ptr_test_helper.h"
     23 
     24 template <int ID = 0>
     25 struct GenericDeleter {
     26   void operator()(void*) const {}
     27 };
     28 
     29 template <int ID = 0>
     30 struct GenericConvertingDeleter {
     31   template <int OID>
     32   GenericConvertingDeleter(GenericConvertingDeleter<OID>) {}
     33   void operator()(void*) const {}
     34 };
     35 
     36 void test_sfinae() {
     37   { // Disallow copying
     38     using U1 = std::unique_ptr<A[], GenericConvertingDeleter<0> >;
     39     using U2 = std::unique_ptr<A[], GenericConvertingDeleter<1> >;
     40     static_assert(std::is_constructible<U1, U2&&>::value, "");
     41     static_assert(!std::is_constructible<U1, U2&>::value, "");
     42     static_assert(!std::is_constructible<U1, const U2&>::value, "");
     43     static_assert(!std::is_constructible<U1, const U2&&>::value, "");
     44   }
     45   { // Disallow illegal qualified conversions
     46     using U1 = std::unique_ptr<const A[]>;
     47     using U2 = std::unique_ptr<A[]>;
     48     static_assert(std::is_constructible<U1, U2&&>::value, "");
     49     static_assert(!std::is_constructible<U2, U1&&>::value, "");
     50   }
     51   { // Disallow base-to-derived conversions.
     52     using UA = std::unique_ptr<A[]>;
     53     using UB = std::unique_ptr<B[]>;
     54     static_assert(!std::is_constructible<UA, UB&&>::value, "");
     55   }
     56   { // Disallow base-to-derived conversions.
     57     using UA = std::unique_ptr<A[], GenericConvertingDeleter<0> >;
     58     using UB = std::unique_ptr<B[], GenericConvertingDeleter<1> >;
     59     static_assert(!std::is_constructible<UA, UB&&>::value, "");
     60   }
     61   { // Disallow invalid deleter initialization
     62     using U1 = std::unique_ptr<A[], GenericDeleter<0> >;
     63     using U2 = std::unique_ptr<A[], GenericDeleter<1> >;
     64     static_assert(!std::is_constructible<U1, U2&&>::value, "");
     65   }
     66   { // Disallow reference deleters with different qualifiers
     67     using U1 = std::unique_ptr<A[], Deleter<A[]>&>;
     68     using U2 = std::unique_ptr<A[], const Deleter<A[]>&>;
     69     static_assert(!std::is_constructible<U1, U2&&>::value, "");
     70     static_assert(!std::is_constructible<U2, U1&&>::value, "");
     71   }
     72   {
     73     using U1 = std::unique_ptr<A[]>;
     74     using U2 = std::unique_ptr<A>;
     75     static_assert(!std::is_constructible<U1, U2&&>::value, "");
     76     static_assert(!std::is_constructible<U2, U1&&>::value, "");
     77   }
     78 }
     79 
     80 
     81 int main() {
     82   test_sfinae();
     83 }
     84