[EMCpp]Item-23 Understand std::Move and std::Forward
· ☕ 2 min read
std::move
performs an unconditional cast to an rvalue, while std::forward
casts its argument to an rvalue only if that argument is bound to an rvalue.
std::move
performs an unconditional cast to an rvalue, while std::forward
casts its argument to an rvalue only if that argument is bound to an rvalue.
For std::unique_ptr
pImpl pointers, declare special member functions in the class header, but implement them in the implementation file.
Compared to new
, make functions eliminate source code duplication, improve exception safety, and, for std::make_shard
and std::allocate_shared
, generate code that’s smaller and faster.
Potential use cases for std::weak_ptr
include caching, observer lists, and the prevention of std::shared_ptr
cycles.
std::shared_ptrs
offer convenience approaching that of garbage collection for the shared lifetime management of arbitrary resources.