Click to See Complete Forum and Search --> : memcpy with template


Nokia2280
09-01-2007, 01:47 AM
Why can't we use memcpy with templates? What's wrong with it?

Danny
09-01-2007, 09:54 AM
Almost everything about memcpy is wrong: it doesn't respect object semantics, i.e., it doesn;t copy construct objects during copying but instead, copies raw bytes. This is a problem even in non-template code. The second problem is security. You can easily cause buffer overflows with memcpy( truth be told: you can get such bugs with a zillion other library functions but memcpy is really one of the worst) and finally, it requires nasty reinterpret_cast expressions to convert an object to a byte array and vice versa. Why not use std::copy()? It's so much better than memcpy.