fix MakeUnique
All checks were successful
linux-x64-gcc / linux-gcc (Release) (push) Successful in 1m29s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 58s

This commit is contained in:
tqcq 2024-03-24 11:32:30 +08:00
parent e170bdb974
commit 33723944cc

View File

@ -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