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?
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?
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)
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