Skip to content
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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

gmlueck
Copy link
Contributor

@gmlueck gmlueck commented Dec 6, 2024

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__.

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
Copy link
Collaborator

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?

Copy link
Contributor Author

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.

Copy link
Collaborator

@nliber nliber Feb 6, 2025

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++).

Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants