…two different addresses are printed, which means the whole object was unnecessarily copied.
For cases like this, it’s probably better to have an in-place init function (fn init(self: *BigStruct, v: u64) void), but how can i tell when this is necessary and copy elision is not possible?
Why are return-by-value init functions even so common in zig and used everywhere in the std library, as opposed to C and C++ where you would normally initialize objects in-place?
I’m not sure that’s the issue. It looks like the external call to print is throwing the optimizer off. If we replace it with a function where the compiler has full visibility, everything behaves as expected (godbolt)
The same poorly optimized code happens with an equivalent C code compiled with GCC: godbolt.
It could be that the compiler thinks print is going to modify the BigStruct, even though it’s marked as const.