Используйте std :: copy, если профилирование не показывает вам нужную пользу в противном случае. Он чтит инкапсуляцию объектов C++, вызывая конструкторы копирования и операторы присваивания, а реализация может включать в себя другие встроенные оптимизации, такие как отказ от вызова функции вне сети для memcpy(), если размер известен во время компиляции и слишком мал для оправдывайте служебные данные вызова функции. (Некоторые системы могут иметь макросы memcpy, которые делают аналогичные определения, но в целом компилятор C++ будет иметь более полное представление о том, какие оптимизации функционально эквивалентны.)
FWIW/на старом Linux-поле у меня есть, GCC не делает любые зрелищные оптимизации, но бит/type_traits.h делает позволяет программу легко определить, следует ли станд :: копия упадет до тетсра():
* 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());
}
*/
Кажется, что 'std :: copy' скорее использует' std :: memmove', потому что диапазоны позволяют перекрываться (на одном конце). – visitor
@visitor: Наверное, правда. Но я уверен, что memmove() вызывает memcpy(), если он определяет, что диапазоны не перекрываются (простая арифметика указателя). –
Я видел реализацию memmove, которая просто делает копию назад, если перекрытие вызовет проблемы. – doron