You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[BUG] Permit operator= with in this (via function metafunction @proxy) #452
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.
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?
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.
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 constoperator=, and behaves like a value type.
It has:
T operator=(T) constnoexcept;
🔗
T operator=(T desired) const noexcept; 11 #
Effects: Equivalent to: store(desired);
return desired;
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.
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.
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]]
}
changed the title [-][BUG] Permit `operator=` with `in this`[/-][+][BUG] Permit `operator=` with `in this` (via function metafunction `@proxy`)[/+]on Aug 1, 2023
Activity
jcanizales commentedon May 26, 2023
What is the use case for an assignment on a const object of type pair?
JohelEGP commentedon May 26, 2023
It was added for:
And I think it's also used for:
Rationale seems to be, in order, at
jcanizales commentedon May 26, 2023
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 commentedon May 30, 2023
Another use-case is
std::ignore: https://en.cppreference.com/w/cpp/utility/tuple/ignore.Dooez commentedon 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 commentedon Jun 29, 2023
Because
constdata members have pitfalls, we use pointers instead.See https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rc-constref.
Dooez commentedon 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=(...) constare 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 orzip_viewreference or a tuple of references(for example fromstd::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 commentedon Jun 29, 2023
Class template
atomic_refis one example for which such requirement wouldn't work.See https://eel.is/c++draft/atomics.ref.generic.
It has a
constoperator=, and behaves like a value type.It has:
🔗
T operator=(T desired) const noexcept;
11
#
Effects: Equivalent to: store(desired);
return desired;
JohelEGP commentedon Jun 29, 2023
Or maybe not.
It has
atomic_ref& operator=(const atomic_ref&) = delete;.JohelEGP commentedon Jun 29, 2023
Maybe https://eel.is/c++draft/template.slice.array is a better example.
It seems there are other
*_arrays accompanyingvalarraywith reference semantics that are copyable.JohelEGP commentedon Jun 29, 2023
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 commentedon Jun 29, 2023
According to the link
So they are not copy assignable if i'm not mistaken.
Dooez commentedon 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 commentedon Jun 29, 2023
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='sthiscan't be aninparameter.Instead, I'd suggest adding the function metafunction
@proxy.That makes for a more descriptive interface.
[-][BUG] Permit `operator=` with `in this`[/-][+][BUG] Permit `operator=` with `in this` (via function metafunction `@proxy`)[/+]