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.
These functions are more general.
memcpy
andmemset
are only defined for contiguous ranges of types that are TriviallyCopyable, andmemcmp
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 doesmemcpy
when it can prove the ranges don't overlap, ormemmove
otherwise. Similarlystd::fill
withmemset
andstd::equal
withmemcmp
1.