I'm testing with this code:
import unittest
type
X = object
filler: array[2048, int]
innerAddress: uint
proc initX(): X =
result.innerAddress = cast[uint](result.addr)
proc initXInPlace(x: var X) =
x.innerAddress = cast[uint](x.addr)
test "NRVO1":
var x = initX()
let innerAddress = x.innerAddress
let outerAddress = cast[uint](x.addr)
check(innerAddress == outerAddress)
test "NRVO2":
var x: X
initXInPlace(x)
let innerAddress = x.innerAddress
let outerAddress = cast[uint](x.addr)
check(innerAddress == outerAddress)
I've read that using result guarantees NRVO to take place, so I was hoping initX and initXInPlace would be equivilant, resulting in no copying of type X resulting in innerAddress and outerAddress staying the same However it seems that doesn't happen, because test "NRVO1" fails while "NRVO2" succeeds
I added the filler field just to check if the object needed to be of a sufficient size, but no luck there either