Utilice std :: copy a menos que la creación de perfiles le muestre un beneficio necesario para hacer lo contrario. Hace honor a la encapsulación de objetos C++, invocando constructores de copia y operadores de asignación, y la implementación podría incluir otras optimizaciones en línea como evitar una llamada a la función fuera de línea a memcpy() si el tamaño es conocido en tiempo de compilación y demasiado pequeño justifica la función llamada sobrecarga. (Algunos sistemas pueden tener macros memcpy que hacen determinaciones similares, pero en general el compilador C++ tendrá más información sobre qué optimizaciones son funcionalmente equivalentes).
FWIW/en la vieja caja Linux Tengo a mano, GCC no lo hace las optimizaciones espectacular, pero bits/type_traits.h hace que el programa pueda especificar fácilmente si std :: copia debe caer a través de memcpy():
* Copyright (c) 1997
* Silicon Graphics Computer Systems, Inc.
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation. Silicon Graphics makes no
* representations about the suitability of this software for any
* purpose. It is provided "as is" without express or implied warranty.
...
/*
This header file provides a framework for allowing compile time dispatch
based on type attributes. This is useful when writing template code.
For example, when making a copy of an array of an unknown type, it helps
to know if the type has a trivial copy constructor or not, to help decide
if a memcpy can be used.
The class template __type_traits provides a series of typedefs each of
which is either __true_type or __false_type. The argument to
__type_traits can be any type. The typedefs within this template will
attain their correct values by one of these means:
1. The general instantiation contain conservative values which work
for all types.
2. Specializations may be declared to make distinctions between types.
3. Some compilers (such as the Silicon Graphics N32 and N64 compilers)
will automatically provide the appropriate specializations for all
types.
EXAMPLE:
//Copy an array of elements which have non-trivial copy constructors
template <class _Tp> void
copy(_Tp* __source,_Tp* __destination,int __n,__false_type);
//Copy an array of elements which have trivial copy constructors. Use memcpy.
template <class _Tp> void
copy(_Tp* __source,_Tp* __destination,int __n,__true_type);
//Copy an array of any type by using the most efficient copy mechanism
template <class _Tp> inline void copy(_Tp* __source,_Tp* __destination,int __n) {
copy(__source,__destination,__n,
typename __type_traits<_Tp>::has_trivial_copy_constructor());
}
*/
Parece que 'std :: copy' usa' std :: memmove' porque los rangos se pueden solapar (en un extremo). – visitor
@visitor: Probablemente cierto. Pero apuesto a que memmove() llama a memcpy() si determina que los rangos no se superponen (aritmética del puntero fácil). –
He visto una implementación de memmove que simplemente hace la copia hacia atrás si la superposición causaría problemas en el futuro. – doron