Add missing make_unique overload.
authorJon Purdy <jonp@fb.com>
Sat, 31 May 2014 00:10:38 +0000 (17:10 -0700)
committerAnton Likhtarov <alikhtarov@fb.com>
Mon, 9 Jun 2014 22:35:47 +0000 (15:35 -0700)
Summary: C++14 adds this overload but I wanted it today.

Test Plan: It compiles, and this is the definition described in the standard.

Reviewed By: xning@fb.com

Subscribers: folly@lists

FB internal diff: D1338839

folly/Memory.h

index 8394f6ad24c3ce55414515f4b2a5c2ea2ab900e4..625c466eef4f6a590e6405ddb96e32748e6828b0 100644 (file)
@@ -38,10 +38,24 @@ namespace folly {
  */
 
 template<typename T, typename Dp = std::default_delete<T>, typename... Args>
  */
 
 template<typename T, typename Dp = std::default_delete<T>, typename... Args>
-std::unique_ptr<T, Dp> make_unique(Args&&... args) {
+typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T, Dp>>::type
+make_unique(Args&&... args) {
   return std::unique_ptr<T, Dp>(new T(std::forward<Args>(args)...));
 }
 
   return std::unique_ptr<T, Dp>(new T(std::forward<Args>(args)...));
 }
 
+// Allows 'make_unique<T[]>(10)'. (N3690 s20.9.1.4 p3-4)
+template<typename T, typename Dp = std::default_delete<T>>
+typename std::enable_if<std::is_array<T>::value, std::unique_ptr<T, Dp>>::type
+make_unique(const size_t n) {
+  return std::unique_ptr<T, Dp>(new typename std::remove_extent<T>::type[n]());
+}
+
+// Disallows 'make_unique<T[10]>()'. (N3690 s20.9.1.4 p5)
+template<typename T, typename Dp = std::default_delete<T>, typename... Args>
+typename std::enable_if<
+  std::extent<T>::value != 0, std::unique_ptr<T, Dp>>::type
+make_unique(Args&&...) = delete;
+
 /**
  * A SimpleAllocator must provide two methods:
  *
 /**
  * A SimpleAllocator must provide two methods:
  *