Add MakeUnique.h so make_unique is available without C++14

Includes make_unique from C++14 and make_unique_default_init from C++20,
because it doesn't require compiler support
This commit is contained in:
Silent 2019-08-11 13:27:46 +02:00
parent 8643360438
commit cb99f65c96
No known key found for this signature in database
GPG Key ID: AE53149BB0C45AF1
2 changed files with 43 additions and 0 deletions

View File

@ -431,6 +431,7 @@
<ClInclude Include="KeyMap.h" />
<ClInclude Include="Log.h" />
<ClInclude Include="LogManager.h" />
<ClInclude Include="MakeUnique.h" />
<ClInclude Include="MathUtil.h" />
<ClInclude Include="MemArena.h" />
<ClInclude Include="MemoryUtil.h" />

42
Common/MakeUnique.h Normal file
View File

@ -0,0 +1,42 @@
#pragma once
#include <memory>
#include <type_traits>
// Custom make_unique so that C++14 support will not be necessary for compilation
template<class T, class... Args,
typename std::enable_if<!std::is_array<T>::value, int>::type = 0>
std::unique_ptr<T> make_unique(Args&&... args)
{
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
template<class T,
typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0, int>::type = 0>
std::unique_ptr<T> make_unique(std::size_t size)
{
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]());
}
template<class T, class... Args,
typename std::enable_if<std::extent<T>::value != 0, int>::type = 0>
void make_unique(Args&&... args) = delete;
template<class T,
typename std::enable_if<!std::is_array<T>::value, int>::type = 0>
std::unique_ptr<T> make_unique_default_init()
{
return std::unique_ptr<T>(new T);
}
template<class T,
typename std::enable_if<std::is_array<T>::value && std::extent<T>::value == 0, int>::type = 0>
std::unique_ptr<T> make_unique_default_init(std::size_t size)
{
return std::unique_ptr<T>(new typename std::remove_extent<T>::type[size]);
}
template<class T, class... Args,
typename std::enable_if<std::extent<T>::value != 0, int>::type = 0>
void make_unique_default_init(Args&&... args) = delete;