You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The iostream library offers a classical interface for binary read/write, which requires a char* and a size. This is an error-prone interface. Besides it does not integrate well with buffers represented as spans of std::byte.
There are, at least, a couple of alternatives that could be integrated in the GSL:
Provide wrapper functions for read() and write().
Provide a function as_buffer along the lines of the as_bytes function in Programming Principles and Practice 2nd Ed (Bjarne Stroustrup).
Wrapper functions
We might offer global functions
template <typename T>
voidread(istream & is, T & value);
template <typename T>
voidwrite(ostream & os, T const & value);
We might need specializations for specific types (e.g.: span)
Buffer convenience function
Programming Principles and Practice offers the following function:
template<classT>
char* as_bytes(T& i) // treat a T as a sequence of bytes
{
void* addr = &i; // get the address of the first byte// of memory used to store the objectreturnstatic_cast<char*>(addr); // treat that memory as bytes
}
We might name such a function as_buffer.
An addition might be to offer an specialization when T is a span of bytes.
The text was updated successfully, but these errors were encountered:
Hi @jdgarciauc3m,
Thanks for submitting this issue 😃. Since the questions here are discussing interfaces that aren't fully specified by the Core Guidelines, I've forwarded the question to that repo (isocpp/CppCoreGuidelines#2067), along with some thoughts/comments.
Maintainers call: The Core Guidelines maintainers prefer not to add individual span overloads/wrappers for standard library interfaces that take raw pointers and lengths, that should be in the standard. We've asked the standards committee to do this and there is interest in making span interfaces throughout the standard library including this example.
The iostream library offers a classical interface for binary read/write, which requires a char* and a size. This is an error-prone interface. Besides it does not integrate well with buffers represented as spans of std::byte.
There are, at least, a couple of alternatives that could be integrated in the GSL:
as_buffer
along the lines of theas_bytes
function in Programming Principles and Practice 2nd Ed (Bjarne Stroustrup).Wrapper functions
We might offer global functions
We might need specializations for specific types (e.g.:
span
)Buffer convenience function
Programming Principles and Practice offers the following function:
We might name such a function
as_buffer
.An addition might be to offer an specialization when
T
is aspan
ofbytes
.The text was updated successfully, but these errors were encountered: