Move semantics for a string are interesting as the data type involves a reference: a pointer to a usually byte array. For efficiency, it would be preferable to not have to do a copy + destructor and instead move the data entirely to a new string type via simple memcpy.
Is this a reasonable way to implement string move semantics?
Code: Select all
#include "crt.bi"
dim as ubyte fromString(sizeof(string)-1)
dim as ubyte toString(sizeof(string)-1)
*cast(string ptr, @toString(0)) = ""
*cast(string ptr, @fromString(0)) = "move me"
'Move the string structure
memcpy(@toString(0), @fromString(0), sizeof(string))
'Erase the data in the old data container
memset(@fromString(0), 0, sizeof(string))
print *cast(string ptr, @toString(0))
print *cast(string ptr, @fromString(0))
sleep