-1

I get the follow error:

'make_unique' is not a member of 'std'

while it write the follow code:
std::make_unique()<Obj>(tmp)
How can I fix it that it will be ok in c++11?

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
MathQues
  • 129
  • 1
  • 10

3 Answers3

6

First, std::make_unique()<Obj>(tmp) is incorrect syntax, it should be std::make_unique<Obj>(tmp) instead.

Second, std::make_unique() does not exist in C++11, it was added in C++14 (unlike std::make_shared(), which does exist in C++11).

If you look at the cppreference doc for std::make_unique(), it shows a possible implementation that (with minor tweaks) can be applied to C++11 code. If your code doesn't need to worry about std::unique<T[]> support for arrays, then the simplest implementation would look like this:

template<class T, class... Args>
std::unique_ptr<T> make_unique(Args&&... args)
{
    return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}

Then you can use (without the std:: prefix):

make_unique<Obj>(tmp)

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Can you write the possible implementation that (with minor tweaks) can be applied to C++11 code ? – MathQues Sep 24 '20 at 21:59
  • 1
    See the update to my answer. If you want array support, the only tweak I see being needed for C++11 in the [cppreference example](https://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique) is changing `std::enable_if_t<...>` to `std::enable_if<...>::type`, since `std::enable_if_t` doesn't exist in C++11 either, it was also added in C++14. – Remy Lebeau Sep 24 '20 at 22:01
0

You can't: make_unique was added in C++14.

Alex Huszagh
  • 13,272
  • 3
  • 39
  • 67
0

EDIT: Boost included in Centos 7 is version 1.53, which is too old and does not have boost::make_unique. It appeared in Boost version 1.56.0. So the below probably isn't all that helpful. You could install a newer version of Boost manually, which is more work than just copying the std::make_unique implementation from this answer: https://stackoverflow.com/a/12580468/912307


The Boost library has an implementation boost::make_unique.

It seems to move around a bit in the library depending on version. If you are on a system with an old compiler (f.ex. CentOS 7) you probably also have an old version of Boost.

In 1.57.0 it is in <boost/move/make_unique.hpp> https://www.boost.org/doc/libs/1_57_0/doc/html/move/reference.html#header.boost.move.make_unique_hpp

In 1.63.0 it is in <boost/make_unique.hpp> https://www.boost.org/doc/libs/1_63_0/libs/smart_ptr/make_unique.html

In 1.7.5 it is in <boost/smart_ptr/make_unique.hpp> https://www.boost.org/doc/libs/1_75_0/libs/smart_ptr/doc/html/smart_ptr.html#make_unique

Åsmund
  • 1,332
  • 1
  • 15
  • 26