AcidFaucet wrote:
Quote:
The C++ equivalent of memcpy() is std::copy(). Never use memcpy() in C++.
Ah ah ah, never said I don't use something similar to std::copy() where appropriate. It's outright stupid to do for floats.
As I said, std::copy() will behave identically to memcpy() for POD types (such as float), through the magic of template specialisation. Plus, std::copy can handle overlapping source and destination (a la memmove).
Quote:
I wouldn't call 'unaware of copy-constructors' a bad thing. I'd be more inclined to call copy-constructors bad, the very reason for righting a NOCOPYDEF macro. Memcpy can't fail miserably unless you do something really silly with dlls, threads, or inter-process comm. If it can, I'd love to see a piece of code that will do it.
Any class that manages resources is incompatible with memcpy(), because the copy-constructor and destructor won't be called. As a trivial example, you can't memcpy a boost::smart_ptr, or any std::library container:
Code:
std::vector< std::vector<int> > vec1, vec2;
vec1.resize( 100 );
vec2.resize( 100 );
memcpy( &vec2[0], &vec1[0], 100*sizeof(std::vector<int>) ); // mucho bad, because we just leaked several internal buffers, and will later double-free the rest