-
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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, andstd::stringdon't have this, I'm wondering if there is any trap or not./cc @ywkaras @SolidWallOfCode
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:
It's ambiguous whether the prototype for foo() is
foo(char *)orfoo(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.
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.
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
memcpyI would overload such functions to take advantage of knowing the size implicitly.