-
Notifications
You must be signed in to change notification settings - Fork 844
Add transparent access for internal buffer in LocalBuffer #6659
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
Conversation
64d662f to
315577b
Compare
| T &operator[](std::size_t index); | ||
| const T &operator[](std::size_t index) const; | ||
|
|
||
| operator T *(); |
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.
Because of std::array, std::vector, and std::string don't have this, I'm wondering if there is any trap or not.
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.
Can you give examples where this would be useful?'
This is a conversion operator, which (without the explicit keyword) creates an implicit conversion. This, https://stackoverflow.com/questions/2346083/why-implicit-conversion-is-harmful-in-c , talks about implicit conversion due to constructors, but the issues also apply to conversion operators.
To give a simple example of how it's bad:
LocalBuffer<char> buf;
// ...
foo(buf);
It's ambiguous whether the prototype for foo() is foo(char *) or foo(LocalBuffer<char> &).
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 implicit conversion is to not introduce additional code, which you can find on the existing unit tests and #6536.
uint8_t *buf = local_buffer.data();
As you can see on the unit tests I added, you would be able to pass a LocalBuffer to memcpy (and functions that expects a pointer for an array).
My understanding of the intent of using LocalBuffer is achieving code like below.
char buf[max(1024, given_dynamic_size)];
So making LocalBuffer usable just like an array without additional code or explicit conversion makes sense to me. I don't think we will want functions that receives LocalBuffer.
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.
Note that the partial specialization of std::unique_ptr can be used for variable sized buffers. But the designers of this chose not to make it implicitly convert to a raw pointer. https://godbolt.org/z/ozKPso
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 guess it's because implicit conversion can easily break the concept of ownership.
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.
Yes. I have to agree with removing the implicit conversion and requiring a call to data(). That makes it consistent with the other types of containers (e.g. string_view, string, etc.).
If you want to pass directly to things like memcpy I would overload such functions to take advantage of knowing the size implicitly.
|
If we want to make the conversion explicit I think it should not provide index access neither since you can do |
#6536 looks ok, but use of LocalBuffer requires additional variable just for type conversion. This PR adds transparent access for the internal buffer in LocalBuffer and it allows accessing LocalBuffer like a pure array.