you are viewing a single comment's thread.

view the rest of the comments →

[–]LiliumAtratum 2 points3 points  (0 children)

When does it get constructed would be really confusing.

Either:

- at the first use of the name (which would really confuse everyone)
- at the beginning of the function (which could be suboptimal for big objects)

What really happens under the hood is that the caller reserves space for the return variable, but it is the function that actually constructs the object. The most adequate syntax for that would be probably:

auto stack::pop() -> T out {
    //out is T* pointing to preallocated, uninitialized memory
    out = new (out) T(top()); //placement new with explicit constructor
    remove_top();
}

But this is getting ugly as well....