Skip to content

Commit

Permalink
Add support for C++11 compilation of std::make_unique() (#470)
Browse files Browse the repository at this point in the history
  • Loading branch information
atanisoft authored Nov 22, 2020
1 parent 7311f43 commit 2bcdef3
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/openlcb/BulkAliasAllocator.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "openlcb/BulkAliasAllocator.hxx"
#include "openlcb/CanDefs.hxx"
#include "utils/MakeUnique.hxx"

namespace openlcb
{
Expand Down
50 changes: 50 additions & 0 deletions src/utils/MakeUnique.hxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* \file MakeUnique.hxx
*
* C++11 version of std::make_unique which is only available from c++14 or
* later.
*
* This is based on https://isocpp.org/files/papers/N3656.txt.
*
* The __cplusplus constant reference is from:
* http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2014/n3938.html
*
*/

// Check if we are building with less than C++14 and if so we need to define
// the std::make_unique() API.
#if __cplusplus < 201402L

#include <memory>
#include <type_traits>
#include <utility>

namespace std
{

template <typename T, typename... Args>
unique_ptr<T> make_unique_helper(false_type, Args&&... args)
{
return unique_ptr<T>(new T(forward<Args>(args)...));
}

template <typename T, typename... Args>
unique_ptr<T> make_unique_helper(true_type, Args&&... args)
{
static_assert(extent<T>::value == 0,
"make_unique<T[N]>() is forbidden, please use make_unique<T[]>().");

typedef typename remove_extent<T>::type U;
return unique_ptr<T>(new U[sizeof...(Args)]{forward<Args>(args)...});
}

template <typename T, typename... Args>
unique_ptr<T> make_unique(Args&&... args)
{
return make_unique_helper<T>(is_array<T>(), forward<Args>(args)...);
}

}

#endif // __cplusplus < 201402L

0 comments on commit 2bcdef3

Please sign in to comment.