Date: Sat, 2 Apr 2022 00:13:33 +0200
Based on how `operator->` is defined there is no way to return r-value &&.
This cause problem when we use proxy objects like:
```
struct A
{
int v;
};
struct Proxy { A a; A* operator->(){ return &a; } };
struct It { Proxy operator->(); };
auto& v = i->v; //no way to avoid dangling reference.
i->v = 5; //write to temp object
```
But if we would allow returning `T&` or `T&&` many this problems could
be solved.
I suggest that we add a new operator, `operator->(int)` . It will work
as a "terminal" `operator->`.
It will take over the role of buildin `->`. It will work the same way
as `operator*` but the only difference is it participates in the `->`
chain.
Fixed code will work like:
```
struct Proxy { A a; A operator->(int){ return std::move(a); } }; //can
return `A&` or `A&&` too
struct It { Proxy operator->(); }
auto& v = i->v; //life time extension, same as for (*i).v
i->v = 5; //compilation error: using rvalue as lvalue, same like
similar (*i).v = 5;
```
This cause problem when we use proxy objects like:
```
struct A
{
int v;
};
struct Proxy { A a; A* operator->(){ return &a; } };
struct It { Proxy operator->(); };
auto& v = i->v; //no way to avoid dangling reference.
i->v = 5; //write to temp object
```
But if we would allow returning `T&` or `T&&` many this problems could
be solved.
I suggest that we add a new operator, `operator->(int)` . It will work
as a "terminal" `operator->`.
It will take over the role of buildin `->`. It will work the same way
as `operator*` but the only difference is it participates in the `->`
chain.
Fixed code will work like:
```
struct Proxy { A a; A operator->(int){ return std::move(a); } }; //can
return `A&` or `A&&` too
struct It { Proxy operator->(); }
auto& v = i->v; //life time extension, same as for (*i).v
i->v = 5; //compilation error: using rvalue as lvalue, same like
similar (*i).v = 5;
```
Received on 2022-04-01 22:13:45