CppCoreGuidelines
CppCoreGuidelines copied to clipboard
gsl::copy is not in the Core Guidelines
Microsoft's GSL currently has a definition for the gsl::copy
function. However, gsl::copy
isn't properly mentioned anywhere in the Core Guidelines, including the GSL: Guidelines support library section.
Should this function be added to the CG with a specification and corresponding rule?
History of the current implementation
gsl::copy
was first brought up in std::copy doesn't work on 2 span · Issue #248 · microsoft/GSL (github.com), in reference to an example code snippet mentioned in I.13: Do not pass an array as a single pointer:
Alternative Consider using explicit spans:
void copy(span<const T> r, span<T> r2); // copy r to r2
It was then implemented in PR Add a copy function for span as mentioned in issue #248 by MikeGitb · Pull Request #344 · microsoft/GSL (github.com), eventually leading to the current implementation in Microsoft's GSL.
Note: there is also another instance of this idea in I.24: Avoid adjacent parameters that can be invoked by the same arguments in either order with different meaning, except with the name name copy_n
:
Alternative Don’t pass arrays as pointers, pass an object representing a range (e.g., a
span
):void copy_n(span<const T> p, span<T> q); // copy from p to q
Editors call: Yes, we should add a note to the GSL specification that copy(span<const T>, span<T>)
be provided in GSL.
It may also be worth mentioning that using std::copy
on gsl::span
iterators causes pathetic performance on major compilers except for MSVC, which has a special workaround. Using std::copy
, there are boundary checks in each copy step. Using gsl::copy
, the boundary checks are done only at the beginning.