What ever happened to move semantics?

Walter Bright newshound2 at digitalmars.com
Wed Feb 28 20:38:53 UTC 2024


On 2/28/2024 6:22 AM, Timon Gehr wrote:
> On 2/28/24 15:17, Timon Gehr wrote:
>> ...
>> What is missing from the DIP?
>> ...
>>
> 
> 19. Explicit moves.
> 
> There is no way to force that a given occurrence of a variable is the last use 
> and is moved. We can use std.algorithm.move, but the DIP as specified would just 
> move a copy if something is used again.

Forcing a move just sounds like trouble. If it is not the last use, and it is 
moved, then wouldn't the result be undefined behavior? Determining last use 
should be in the purview of the compiler, not the user, so it is reliable.

The Ownership/Borrowing system does determine last use, using data flow analysis.


> What I would like to see is:
> 
> ```d
> void main(){
>      S s;
>      foo(move(s));
>      auto t=s; // error, `s` has been moved
>      S s; // ok, can redeclare `s`
> }
> ```
> 
> Maybe there needs to be a parameter annotation that forces a move, then `move` 
> can be implemented in terms of that.

What is the point of declaring another variable of the same name? We already 
disallow shadowing declarations, and that has prevented a number of bugs at 
least in my own code (they're very difficult to spot with a visual check).


More information about the Digitalmars-d mailing list