Skip to content

[BUG] Permit operator= with in this (via function metafunction @proxy) #452

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
JohelEGP opened this issue May 11, 2023 · 14 comments
Closed
Labels
bug Something isn't working

Comments

@JohelEGP
Copy link
Contributor

Title: Permit operator= with in this.

Description:

operator= sets the value of this object, so the this parameter can be anything but in (which would imply const)
-- https://github.com/hsutter/cppfront/wiki/Cpp2:-operator=,-this-&-that#operator-is-the-name-for-all-value-setting-functions-construction-assignment-conversion-and-destruction.

There are use cases when the target of assignment isn't part of the value of this.
For example: https://eel.is/c++draft/pairs#lib:operator=,pair_.

@jcanizales
Copy link

What is the use case for an assignment on a const object of type pair?

@JohelEGP
Copy link
Contributor Author

@jcanizales
Copy link

Ouch that reads like such a hack... Maybe we could treat it like the weird constructor of vector, and let it happen in C++ code but not be forced to allow someone writing it again in Cppfront.

@JohelEGP
Copy link
Contributor Author

Another use-case is std::ignore: https://en.cppreference.com/w/cpp/utility/tuple/ignore.

@Dooez
Copy link

Dooez commented Jun 29, 2023

This behaviour is useful for proxy references, maybe it should only be allowed if the class is immutable, so no data members are present, or all members are const?

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Jun 29, 2023

Because const data members have pitfalls, we use pointers instead.
See https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-constref.

@Dooez
Copy link

Dooez commented Jun 29, 2023

I suggest restriction of immutability to isolate a situation of assignment being const to cases where it is required.

The types that operator=(...) const are usually used in should not be copy-assignable and movable, so having const members does not affect them in the way described in your link.

Proxy references, such as vector<bool> reference or zip_view reference or a tuple of references(for example from std::tie()) all need to have const assignment operator. But since they are "references" they are immutable, and once constructed cannot be changed. Assigning something to a reference is not intended to change the reference, it is intended to change whatever the reference points to.

So having const data members (for example const pointers) is somewhat approriate in such types, and these types are required to have const assignment.

@JohelEGP
Copy link
Contributor Author

Class template atomic_ref is one example for which such requirement wouldn't work.
See https://eel.is/c++draft/atomics.ref.generic.
It has a const operator=, and behaves like a value type.
It has:

    T operator=(T) const noexcept;

🔗
T operator=(T desired) const noexcept;
11
#
Effects: Equivalent to: store(desired);
return desired;

@JohelEGP
Copy link
Contributor Author

It has a const operator=, and behaves like a value type.

Or maybe not.
It has atomic_ref& operator=(const atomic_ref&) = delete;.

@JohelEGP
Copy link
Contributor Author

Maybe https://eel.is/c++draft/template.slice.array is a better example.
It seems there are other *_arrays accompanying valarray with reference semantics that are copyable.

@JohelEGP
Copy link
Contributor Author

I suppose I'd like to be able to implement view types with deep copy semantics.
I think https://wg21.link/P1085 touches on the topic.

@Dooez
Copy link

Dooez commented Jun 29, 2023

Maybe https://eel.is/c++draft/template.slice.array is a better example. It seems there are other *_arrays accompanying valarray with reference semantics that are copyable.

According to the link

void operator=(const valarray&) const;
const slice_array& operator=(const slice_array&) const;
These assignment operators have reference semantics, assigning the values of the argument array elements to selected elements of the valarray object to which the slice_array object refers.

So they are not copy assignable if i'm not mistaken.

@Dooez
Copy link

Dooez commented Jun 29, 2023

View with deep copy on assignment would not meet view semantic requirements i think.
And at the moment i don't see the need for const deep copy assignment operator.

@JohelEGP
Copy link
Contributor Author

I did not mean a model of std::ranges::view.


Implementing proxies is expert-level.
This is evidenced by the facts that the C++ Core Guidelines
doesn't mention proxies, and
bans one way to implement them with Rc-constref.

So I think it makes sense to keep the restriction that
operator='s this can't be an in parameter.

Instead, I'd suggest adding the function metafunction @proxy.
That makes for a more descriptive interface.

spanning_vector: <T> type = {
  view: * std::vector<T>;
  // Stuff to make the example work.
  operator=: @proxy (this, other: std::vector<T>) = view* = other;
}
main: () = {
  vec1: std::vector<i32>      = (0, 1, 2);
  vec2: std::vector<i32>      = (3, 4, 5, 6, 7);
  view: const spanning_vector = /* Make a view to `vec1`. */;
  view                        = vec2;
  [[assert: vec1 == vec2]]
}

@JohelEGP JohelEGP changed the title [BUG] Permit operator= with in this [BUG] Permit operator= with in this (via function metafunction @proxy) Aug 1, 2023
@JohelEGP JohelEGP closed this as not planned Won't fix, can't repro, duplicate, stale Feb 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants