fix MakeUnique
This commit is contained in:
parent
e170bdb974
commit
33723944cc
@ -1,24 +1,67 @@
|
||||
#ifndef SLED_MAKE_UNIQUE_H
|
||||
#define SLED_MAKE_UNIQUE_H
|
||||
#ifndef SLED_MakeUnique_H
|
||||
#define SLED_MakeUnique_H
|
||||
#pragma once
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
namespace sled {
|
||||
template<typename T, typename... Args>
|
||||
inline auto
|
||||
MakeUnique(Args &&...args) -> std::unique_ptr<T>
|
||||
namespace detail {
|
||||
template<class T>
|
||||
struct remove_extent {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct remove_extent<T[]> {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template<class T, std::size_t N>
|
||||
struct remove_extent<T[N]> {
|
||||
using type = T;
|
||||
};
|
||||
|
||||
template<class>
|
||||
struct is_unbounded_array {
|
||||
// constexpr bool is_unbounded_array_v = false;
|
||||
static constexpr bool value = false;
|
||||
};
|
||||
|
||||
template<class T>
|
||||
struct is_unbounded_array<T[]> {
|
||||
// constexpr bool is_unbounded_array_v<T[]> = true;
|
||||
static constexpr bool value = true;
|
||||
};
|
||||
|
||||
template<class>
|
||||
struct is_bounded_array {
|
||||
// constexpr bool is_bounded_array_v = false;
|
||||
static constexpr bool value = false;
|
||||
};
|
||||
|
||||
template<class T, std::size_t N>
|
||||
struct is_bounded_array<T[N]> {
|
||||
// constexpr bool is_bounded_array_v<T[N]> = true;
|
||||
static constexpr bool value = true;
|
||||
};
|
||||
}// namespace detail
|
||||
|
||||
template<class T, class... Args>
|
||||
typename std::enable_if<!std::is_array<T>::value, std::unique_ptr<T>>::type
|
||||
MakeUnique(Args &&...args)
|
||||
{
|
||||
return std::move(std::unique_ptr<T>(new T(std::forward<Args>(args)...)));
|
||||
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template<typename T, typename U = T>
|
||||
inline typename std::enable_if<!std::is_pointer<T>::value && !std::is_pointer<U>::value && std::is_base_of<T, U>::value,
|
||||
std::unique_ptr<T>>::type
|
||||
MakeUnique(U *ptr)
|
||||
template<class T>
|
||||
typename std::enable_if<detail::is_unbounded_array<T>::value, std::unique_ptr<T>>::type
|
||||
MakeUnique(std::size_t n)
|
||||
{
|
||||
return std::move(std::unique_ptr<T>(ptr));
|
||||
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[n]());
|
||||
}
|
||||
|
||||
template<class T, class... Args>
|
||||
typename std::enable_if<detail::is_bounded_array<T>::value>::type MakeUnique(Args &&...) = delete;
|
||||
|
||||
}// namespace sled
|
||||
#endif// SLED_MAKE_UNIQUE_H
|
||||
#endif// SLED_MakeUnique_H
|
||||
|
Loading…
x
Reference in New Issue
Block a user