25 Oct
2013
25 Oct
'13
11:10 a.m.
2013/10/25 Giovanni Piero Deretta
char buf[N]; T obj; std::memcpy(buf, &obj, N); std::memcpy(&obj, buf, N);
For T = boost::intrusive_ptr<T2>, the above code formally leads to UB, as intrusive_ptr is not trivially copiable.
It might (or not) work in practice, but in the case of std::atomic, it will bypass the copy constructor which means that the reference count won't be updated.
the use-count can be 'adjusted' before, e.g. prevent deallocating the object owned by the intrusive_ptr unintentionally. I'm interested in atomically exchange intrusive-pointers - but anyway I could also use raw-pointers in atomic<> and increment the use-counter of the intrusive_ptr owning the raw-pointer before.