A few posts compare the performance between C++-style functions std::copy, std::equal, std::fill
with C-style functions std::memcpy, std::memcmp, std::memset
, for example :
But I haven't found what justifies the replacement by these new functions over the old one from a practical point of view. Of course, the list of functions is not exhaustive.
What are the practical advantages of these new versions ?
If performance is not a criterion for choosing, is it desirable (time permitting) to replace all occurrences of these C-style functions with the C++ versions ?
I tried to understand why some functions have been introduced in c++
and if it is necessary to do the replacement.
But I haven't found what justifies the replacement by these new functions over the old one from a practical point of view.
These functions are more general. memcpy
and memset
are only defined for contiguous ranges of types that are TriviallyCopyable, and memcmp
is (generally) only meaningful for contiguous ranges of TriviallyCopyable types with no padding. N.b. a single object is vacuously a contiguous range of length one.
Performance is only comparable when they do the same things, and by the as-if rule, an implementation can choose to implement a call to std::copy
in exactly the same way as it does memcpy
when it can prove the ranges don't overlap, or memmove
otherwise. Similarly std::fill
with memset
and std::equal
with memcmp
1.