[fpc-devel] Copy/move operator

Jonas Maebe jonas at freepascal.org
Mon Jun 17 08:19:51 CEST 2019


On 2019-06-16 15:29, Ryan Joseph wrote:
> Basically the problem is that the Copy operator doesn’t know if the
> source value is actually mapped to memory or just a temporary value so
> we’re forced to always copy memory (just to be safe) even when a
> simple move would be good enough. C++ has a similar idea of rvalues
> and lvalues and a copy and move constructor which addresses this.

And which also causes hard-to-find memory corruption bugs.

> Here’s a little example of the problem. What I propose is that we
> either add a 3rd boolean parameter to the Copy operator or add a new
> Move operator which is preferred over the Copy operator if it exists
> (this is much more work obviously but maybe has some benefit).

The compiler knows that source is a temporary value that will be gone 
afterwards, so it could perform this optimisation itself in this case. 
It won't be possible to always do this and there are undoubtedly cases 
where a dedicated Move operator would result in more optimal code, but 
maintainability and safety are also important.


Jonas


More information about the fpc-devel mailing list