Sử dụng tiêu chuẩn :: sao chép trừ khi tiểu sử hiển thị cho bạn lợi ích cần thiết khi thực hiện khác. Nó tôn vinh việc đóng gói đối tượng C++, gọi các hàm tạo bản sao và các toán tử gán, và việc triển khai có thể bao gồm các tối ưu hóa nội tuyến khác như tránh một cuộc gọi hàm ngoài dòng lệnh memcpy() nếu kích thước được biết tại thời gian biên dịch và quá nhỏ biện minh cho phí gọi hàm. (Một số hệ thống có thể có macro memcpy tạo ra các xác định tương tự, nhưng nói chung trình biên dịch C++ sẽ có cái nhìn sâu hơn về những gì tối ưu hóa là tương đương về chức năng.)
FWIW/trên hộp Linux cũ tôi có ích, GCC không làm bất kỳ tối ưu hóa ngoạn mục nào, nhưng bit/type_traits.h không cho phép chương trình dễ dàng xác định xem std :: copy có rơi vào 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());
}
*/
Nguồn
2010-09-13 09:57:03
Có vẻ như 'std :: copy' thay vì sử dụng' std :: memmove' vì phạm vi được phép chồng chéo (ở một đầu). – visitor
@visitor: Có thể là sự thật. Nhưng tôi đặt cược memmove() gọi memcpy() nếu nó xác định phạm vi không chồng chéo (số học con trỏ dễ dàng). –
Tôi đã thấy một triển khai memmove mà chỉ cần làm sao chép ngược nếu chồng chéo sẽ gây ra vấn đề về phía trước. – doron