I have written a vector class to learn move semantics. I use move constructor to move T (commented line).
My question is why not to just copy all the bytes of temp object and set all bytes of temp object to zero?
I know that the destructor will be called for temp object and it may require some initialized members to properly destruct. This can be reason why I must not change internal representation of the object.
But in case I'm 100% sure my ~T() don't have such requirements, is this a good optimization?
20 void push_back(T&& val)
21 {
22 check_cap();
23 //new (m_data + m_size) T(std::move(val));
24 for(int i = 0; i < sizeof(T); ++i)
25 {
26 reinterpret_cast<char*> (m_data + m_size)[i] = reinterpret_cast<char*> (&val)[i];
27 reinterpret_cast<char*> (&val)[i] = 0;
28 }
29 m_size++;
30 }
(please don't tell anything about casts and their safety if it's not related to the actual question)
Aucun commentaire:
Enregistrer un commentaire