move semantics - c++ type trait to say "trivially movable" - examples of -
i define "trivially movable" by
calling move constructor (or move assignment operator) equivalent memcpy bytes new destination , not calling destructor on moved-from object.
for instance, if know property holds, can use realloc
resize std::vector or memory pool.
types failing typically have pointers contents needs updated move constructor/assignment operator.
there no such type traits in standard can find. wondering whether has (better) name, whether it's been discussed , whether there libraries making use of such trait.
edit 1:
from first few comments, std::is_trivially_move_constructible
, std::is_trivially_move_assignable
not equivalent looking for. believe give true
types containing pointers themselves, since reading own member seems fall under "trivial" operation.
edit 2:
when implemented, types point won't trivially_move_constructible or move_assignable because move ctor / move assignment operator not trivial anymore. though, ought able unique_ptr can safely copied new location provided don't call destructor.
well, got me thinking... important overload type traits of structs hold pointer themselves.
the following code demonstrates how fast bug can creep in code, when type_traits not defined properly.
#include <memory> #include <type_traits> struct { int a; int b; int* p{&a}; }; int main() { auto p = std::make_unique<a>(); a = std::move(*p.get()); // gets moved here, a.p dangling. return std::is_move_assignable<a>::value; // <-- yet, returns true. }
Comments
Post a Comment