-
Notifications
You must be signed in to change notification settings - Fork 69
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
Specify copy assignment operator for vec swizzles #678
base: main
Are you sure you want to change the base?
Specify copy assignment operator for vec swizzles #678
Conversation
Even though we specify an assignment operator for `__writeable_swizzle__` like this: ``` template</*unspecified*/> const __writeable_swizzle__& operator=(const __writeable_swizzle__</*unspecified*/>& rhs) const ``` The compiler will not select this operator when the left-hand-side has the same type as the right-hand-side. (Even though the template constraints would allow such a selection.) Instead, the compiler will generate the default copy assignment operator and call that. Since the default copy assignment operator would not be correct (performing a shallow copy), the `__writeable_swizzle__` class must provide a user defined copy assignment operator. The copy assignment operator for `__const_swizzle__` was already deleted in the "constructors" section. It seemed better to move this to the "member functions" part of the specification, next to the specification for the copy assignment operator for `__writeable_swizzle__`.
[source] | ||
---- | ||
const __writeable_swizzle__& | ||
operator=(const __writeable_swizzle__& rhs) const |
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.
Why is this a const
member function?
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 the swizzle class is a "view". The assignment operator doesn't change the swizzle itself, it changes the underlying vec
.
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.
It is still logically a non-const
operation.
There is precedent in C++ for this, such as ostream_iterator::operator=(const T&)
(as well as some of its other operators, like operator*
and operator++
).
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.
Discussed at the WG meeting 2025-02-06. span does similar things with operator[]. No change requested.
Even though we specify an assignment operator for
__writeable_swizzle__
like this:The compiler will not select this operator when the left-hand-side has the same type as the right-hand-side. (Even though the template constraints would allow such a selection.) Instead, the compiler will generate the default copy assignment operator and call that.
Since the default copy assignment operator would not be correct (performing a shallow copy), the
__writeable_swizzle__
class must provide a user defined copy assignment operator.The copy assignment operator for
__const_swizzle__
was already deleted in the "constructors" section. It seemed better to move this to the "member functions" part of the specification, next to the specification for the copy assignment operator for__writeable_swizzle__
.