isocpp / isocpp/CppCoreGuidelines
Conversion of spans - love or hate?
@neilmacintosh is already working on this.
Since May 21, 2018.
- Dominant language
- CSS
- Stars
- 45.3k
- Forks
- 5.6k
- PR merge metrics
- No merged PRs in 30d
Description
There is a helper function that some GSL users created for parsing byte streams. This function is currently is not in GSL due to us having problems implementing it efficiently, safely, and in a platform-independent way.
Is general conversion of spans going against the core guidelines?
If yes, is there a limited conversion that is possible (i.e. from std::byte to T)?
What are the checks that need to be done to make it (or a restricted variant) safe?
Are there any platform-specific type layout problems that we need to be aware of?
Are there better ways of achieving the same?
template <class T, class U>
gsl::span<T> convert_span(span<U> s)
{
auto data = s.data();
if (!data)
{
return {};
}
auto bytes = s.size_bytes();
Expects(bytes % sizeof(T) == 0);
return { reinterpret_cast<T*>(data), bytes / narrow_cast<int>(sizeof(T)) };
}
void DoesNotCompile(span<gsl::byte> bytes)
{
gsl::span<int> s(bytes);
}
void Compiles(span<gsl::byte> bytes)
{
gsl::span<int> s = convert_span<int, gsl::byte>(bytes);
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.