-
Notifications
You must be signed in to change notification settings - Fork 737
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a copy function for span as mentioned in issue #248 #344
Conversation
Hi @MikeGitb, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution! TTYL, MSBOT; |
@MikeGitb, Thanks for signing the contribution license agreement so quickly! Actual humans will now validate the agreement and then evaluate the PR. |
I put the function and tests into separate files for now to prevent re-basing conflicts in the future. I'd be happy to move the contents into existing files if that is preferred. |
62f15e3
to
05032e7
Compare
Upon revisiting this PR I realized that returning the remaining destination space is maybe not the most intuitive semantics. It was born from from my primary use case to successively fill a buffer, but that might not be such a common use case in the wild. Possible alternatives:
Any thoughts? |
After seeing this version again in @BjarneStroustrup's keynote slides, I settled - at least for this PR - for the simple version that returns nothing. The question if and what the function should return in addition can then be discussed separately. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for putting this pack together @MikeGitb! Sorry to take so long to get to reviewing it!
I'm still not 100% sure we shouldn't match std::copy_n and return a span that contains the "remaining" area of the destination span. I'll go back and reread your thinking on that and try to form an opinion.
#pragma warning(pop) | ||
#endif | ||
|
||
#endif // GSL_SPAN_H |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think this should be GSL_ALGORITHM_H
|
||
// turn off some misguided warnings | ||
#pragma warning(push) | ||
#pragma warning(disable : 4351) // warns about newly introduced aggregate initializer behavior |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure either of these would fire for this header, so you should be able to remove the whole push/pop sequence.
#define noexcept /*noexcept*/ | ||
#endif | ||
|
||
#pragma push_macro("alignof") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't see alignof
being used (maybe I missed it). But if it is not used, we can get rid of this push/pop sequence.
|
||
template<class SrcElementType, std::ptrdiff_t SrcExtent, | ||
class DestElementType, std::ptrdiff_t DestExtent> | ||
void copy(const span<SrcElementType, SrcExtent> src, span<DestElementType, DestExtent> dest) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The const
on the first span is unnecessary and probably misleading if passing by-value (as you should rightly do here).
#ifdef GSL_THROW_ON_CONTRACT_VIOLATION | ||
|
||
#ifdef _MSC_VER | ||
#pragma push_macro("noexcept") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given there's no noexcept
in this file, I think you can get rid of this push/pop sequence too.
// VS 2013 workarounds | ||
#if _MSC_VER <= 1800 | ||
|
||
#define GSL_MSVC_HAS_VARIADIC_CTOR_BUG |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
None of these define
's should be necessary for this file I hope.
#pragma warning(disable : 26481 26482 26483 26485 26490 26491 26492 26493 26495) | ||
|
||
// No MSVC does constexpr fully yet | ||
#pragma push_macro("constexpr") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no use of constexpr
here so this block and its corresponding "pop" can go.
} | ||
} | ||
|
||
TEST(copy_span_static_diff_type) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for a type that is assignable. Could we have a test that validates that the reverse assignment (for example) does not compile? Similarly, we should probably validate that you can't accidentally slice objects using this function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should slicing really be prevented? Although it is rarely necessary, I actually like the fact, that std::copy / std::copy_n allow copying from one type to another. If that should not be the case, then we probably should make input and output type identical (modulo constness and maybe volatile)
Thanks for the feedback. The header and footer where dump copy past from another file and didn't clean it up afterwards. I'll remove the things you mentioned and expand the tests. |
@neilmacintosh: I adressed some of your points, but not all:
|
@MikeGitb I think you are correct to follow the I had a think about the return value, and although Bjarne However, I can understand if you'd like to take a second PR to add the return type. This has been sitting around for a while ;-) Let me know your preference... |
@neilmacintosh: As the decision about the return type is made it doesn't really matter for me, but if you are fine with the rest, I'd prefer if you merge the PR as it is. Adding the return type is simple, but in case the code and/or unittest changes introduce any issues, I'd like to discuss/fix them separately, as I only work on this very irregularly. |
Makes sense @MikeGitb. I'm happy with everything in this PR, and agree we can always change the return type separately. Thanks for taking on this work! |
Update: The original eiscription is completely outdated -- see the following posts.
Original:
This PR implements a copy function for span similar to what was mentioned in issue #248. However, instead of void, it returns a span representing the remaining space in the destination span.
There are probably a lot of style issues to be addressed and I'd be happy to incorporate any feedback, but my main question at the moment would be if the design is OK and if that function should be put into the
span
header as I did or into some separate utility / algorithms header.