[fpc-devel] Copy/move operator
Ben Grasset
operator97 at gmail.com
Tue Jun 18 16:00:10 CEST 2019
On Mon, Jun 17, 2019 at 5:57 PM Ryan Joseph <genericptr at gmail.com> wrote:
> The copy operator is always called on all assignments. Try actually
> running that and you’ll see. :)
>
Actually, after thinking about it some more, here's what I'd actually write
(in a perfect world where it was possible.)
program Test;
{$mode ObjFPC}
{$modeswitch AdvancedRecords}
type
generic TList<T> = record
public
Data: array of T;
procedure Assign(var Src: TList); inline;
class operator Copy(constref Src: TList; var Dest: TList); //
apparently you cannot actually inline this...
class function Create(const Num: Integer): TList; static; inline;
end;
procedure TList.Assign(var Src: TList);
var Len: SizeInt;
begin
Len := Length(Src.Data);
SetLength(Data, Len);
Move(Src.Data[0], Data[0], SizeOf(T) * Len);
end;
class operator TList.Copy(constref Src: TList; var Dest: TList);
begin
// StaticAssert does not exist, unfortunately...
StaticAssert(False, 'Don't use the := operator, call Assign instead!');
end;
class function TList.Create(const Num: Integer): TList;
begin
SetLength(Result.Data, Num);
end;
begin
// Do whatever here.
end.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20190618/9e5f3e7a/attachment.html>
More information about the fpc-devel
mailing list