The main point of
unique_ptr
is that it eliminates ownership problems. When you have aunique_ptr
you are the only one who has access to an object and do not need to worry about other code. With regular pointers sometimes you must delete them and sometimes you must not, which is confusing. If someone just callsInproc::create();
it is already a memory leak whichunique_ptr
s were meant to prevent:std::unique_ptr<Inproc> create();
Avoid functions such as create, init, destroy, copy and clone. C++ has special member functions for that: Constructor, destructor and copy assignment. They cannot always be used, but if they can prefer them.
Inpoc::create
should actually be a regular constructor. If the construction of anInproc
can fail and you cannot use exceptions provide a member functionbool is_valid() const
that the caller can use to check if construction worked.Prefer
nullptr
overNULL
becausenullptr
works correctly with templates whereasNULL
will occasionally fail.Avoid using
new
anddelete
. To create aunique_ptr
usemake_unique
and forshared_ptr
usemake_shared
. Somehow the C++11 standard forgot to specifymake_unique
so if there is nostd::make_unique
in your STL write your own write your own. You can even write amake_unique_nothrow
for your special requirement.
The main point of
unique_ptr
is that it eliminates ownership problems. When you have aunique_ptr
you are the only one who has access to an object and do not need to worry about other code. With regular pointers sometimes you must delete them and sometimes you must not, which is confusing. If someone just callsInproc::create();
it is already a memory leak whichunique_ptr
s were meant to prevent:std::unique_ptr<Inproc> create();
Avoid functions such as create, init, destroy, copy and clone. C++ has special member functions for that: Constructor, destructor and copy assignment. They cannot always be used, but if they can prefer them.
Inpoc::create
should actually be a regular constructor. If the construction of anInproc
can fail and you cannot use exceptions provide a member functionbool is_valid() const
that the caller can use to check if construction worked.Prefer
nullptr
overNULL
becausenullptr
works correctly with templates whereasNULL
will occasionally fail.Avoid using
new
anddelete
. To create aunique_ptr
usemake_unique
and forshared_ptr
usemake_shared
. Somehow the C++11 standard forgot to specifymake_unique
so if there is nostd::make_unique
in your STL write your own. You can even write amake_unique_nothrow
for your special requirement.
The main point of
unique_ptr
is that it eliminates ownership problems. When you have aunique_ptr
you are the only one who has access to an object and do not need to worry about other code. With regular pointers sometimes you must delete them and sometimes you must not, which is confusing. If someone just callsInproc::create();
it is already a memory leak whichunique_ptr
s were meant to prevent:std::unique_ptr<Inproc> create();
Avoid functions such as create, init, destroy, copy and clone. C++ has special member functions for that: Constructor, destructor and copy assignment. They cannot always be used, but if they can prefer them.
Inpoc::create
should actually be a regular constructor. If the construction of anInproc
can fail and you cannot use exceptions provide a member functionbool is_valid() const
that the caller can use to check if construction worked.Prefer
nullptr
overNULL
becausenullptr
works correctly with templates whereasNULL
will occasionally fail.Avoid using
new
anddelete
. To create aunique_ptr
usemake_unique
and forshared_ptr
usemake_shared
. Somehow the C++11 standard forgot to specifymake_unique
so if there is nostd::make_unique
in your STL write your own. You can even write amake_unique_nothrow
for your special requirement.
(削除) There is no need for I screwed that one up. Apparently you do need the std::move
here because *bound.release()
is already a temporary object. (削除ここまで)move
even though(削除) Apparently you do need the Thanks to @dyp for pointing it out. move
even though * unique_ptr.release()
is logically a temporary. (削除ここまで)* unique_ptr.release()
is logicallycompletely misplaced, that would create a temporarymemory leak. Thanks to dyp for pointingThis time I tested it out.
(削除) There is no need for I screwed that one up. Apparently you do need the std::move
here because *bound.release()
is already a temporary object. (削除ここまで)move
even though * unique_ptr.release()
is logically a temporary. Thanks to dyp for pointing it out.
(削除) There is no need for I screwed that one up. std::move
here because *bound.release()
is already a temporary object. (削除ここまで)(削除) Apparently you do need the Thanks to @dyp for pointing it out. move
even though * unique_ptr.release()
is logically a temporary. (削除ここまで)release
is completely misplaced, that would create a memory leak. This time I tested it.
Would it be possible to have
Inproc(Fan &&bound__, Fan &&connected__);
as constructor and still usestd::unique_ptr
andstd::move
?
You can express that:Inproc(Fan &&bound__, Fan &&connected__); std::unique_ptr<Inproc> Inproc::create(){ std::unique_ptr<Fan> bound(Fan::create()); if (bound){ std::unique_ptr<Fan> connected(Fan::create(bound.get())); if (connected){ //return new(std::nothrow) Inproc(*bound.releasestd::move(*bound), *connected.releasestd::move(*connected)); return make_unique_nothrow<Inproc>(*bound.releasestd::move(*bound), *connected.releasestd::move(*connected)); } } return nullptr; }
There is no(削除) There is no need for I screwed that one up. Apparently you do need forthe std::move
here because *bound.release()
is already a temporary object. (削除ここまで)std::move
here becauseeven though *bound* unique_ptr.release()
is alreadylogically a temporary object. Thanks to dyp for pointing it out.
Would it be possible to have
Inproc(Fan &&bound__, Fan &&connected__);
as constructor and still usestd::unique_ptr
andstd::move
?
You can express that:Inproc(Fan &&bound__, Fan &&connected__); std::unique_ptr<Inproc> Inproc::create(){ std::unique_ptr<Fan> bound(Fan::create()); if (bound){ std::unique_ptr<Fan> connected(Fan::create(bound.get())); if (connected){ //return new(std::nothrow) Inproc(*bound.release(), *connected.release()); return make_unique_nothrow<Inproc>(*bound.release(), *connected.release()); } } return nullptr; }
There is no need for std::move
here because *bound.release()
is already a temporary object.
Would it be possible to have
Inproc(Fan &&bound__, Fan &&connected__);
as constructor and still usestd::unique_ptr
andstd::move
?
You can express that:Inproc(Fan &&bound__, Fan &&connected__); std::unique_ptr<Inproc> Inproc::create(){ std::unique_ptr<Fan> bound(Fan::create()); if (bound){ std::unique_ptr<Fan> connected(Fan::create(bound.get())); if (connected){ //return new(std::nothrow) Inproc(std::move(*bound), std::move(*connected)); return make_unique_nothrow<Inproc>(std::move(*bound), std::move(*connected)); } } return nullptr; }
(削除) There is no need for I screwed that one up. Apparently you do need the std::move
here because *bound.release()
is already a temporary object. (削除ここまで)move
even though * unique_ptr.release()
is logically a temporary. Thanks to dyp for pointing it out.