summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorStanislaw Halik <sthalik@misaki.pl>2016-08-10 15:38:36 +0200
committerStanislaw Halik <sthalik@misaki.pl>2016-08-10 15:38:36 +0200
commitacf53881d880f1a76b1eb799263dc1b2550ad485 (patch)
treed155c4de5f35b72f6158ec6185a2cc3233a0e0b5
parent3afb96603b1c213b5b85689f807a9384f24ada96 (diff)
compat/make-unique: add std::make_unique sample impl
It's not present in GNU with -std=c++14
-rw-r--r--opentrack-compat/make-unique.hpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/opentrack-compat/make-unique.hpp b/opentrack-compat/make-unique.hpp
new file mode 100644
index 00000000..bb5315c5
--- /dev/null
+++ b/opentrack-compat/make-unique.hpp
@@ -0,0 +1,43 @@
+#pragma once
+
+// GNU 5.4.0 doesn't have std::make_unique in -std=c++14 mode
+
+// this implementation was taken from http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3656.htm
+
+#include <memory>
+#include <utility>
+#include <cstddef>
+
+namespace detail {
+template<class T> struct Unique_if
+{
+ typedef std::unique_ptr<T> Single_object;
+};
+
+template<class T> struct Unique_if<T[]>
+{
+ typedef std::unique_ptr<T[]> Unknown_bound;
+};
+
+template<class T, size_t N> struct Unique_if<T[N]>
+{
+ typedef void Known_bound;
+};
+}
+
+template<class T, class... Args>
+ typename detail::Unique_if<T>::Single_object
+ make_unique(Args&&... args) {
+ return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+ }
+
+template<class T>
+ typename detail::Unique_if<T>::Unknown_bound
+ make_unique(std::size_t n) {
+ typedef typename std::remove_extent<T>::type U;
+ return std::unique_ptr<T>(new U[n]());
+ }
+
+template<class T, class... Args>
+ typename detail::Unique_if<T>::Known_bound
+ make_unique(Args&&...) = delete;