-
-
Notifications
You must be signed in to change notification settings - Fork 406
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 at_address() to etl::unaligned_type #999
Add at_address() to etl::unaligned_type #999
Conversation
I think it would be useful to add overloads that include the buffer size, to allow the option for extra safety checks on the buffer size vs the type size..
It would require adding an exception type.
|
I agree. I just added it to the PR. |
Now also added the unit tests. |
The only other thing that slightly bugs me is that I'm a little uncertain as to whether this breaks the 'strict aliasing' rules and do something weird in some circumstances in highly optimised code. I used to do that all the time back when I coded in C and compilers were less able to do sophisticated optimisations, but I'm not so sure about modern C++ compilers. If it is breaking strict aliasing, then I wonder whether an internal placement-new would more compliant? The absolute safe solution would be to initialise an |
In the initially described use case, you always end up reinterpreting data from inside another classes instance. This is true for currently implemented reinterpret_cast, or placement new, or a new class unaligned_type_ext. If you mean new constructors for unaligned_type with copy-by-value, this would cover only the read, not the write. class span suffers from the same dilemma, doesn't it? |
In the containers there are placement-new calls using a pointer to a char array, but the code outside of the container never sees the array, and once placement new occurs, the memory is always accessed as the container's type. In As the raw buffer and internal storage of
It would just be another way of constructing an Another way to do this is to do the same as what I did with the
It could be changed on the fly too.
A span contains pointers to the actual type. |
I agree that an etl::unaligned_type_ext would be more consistent. |
I'm regularly involved with code like this:
To encapsulate the reinterpret_cast, the at_address() is a convenience function to get to a reinterpreted etl::unaligned_type, e.g.