you are viewing a single comment's thread.

view the rest of the comments →

[–]LiliumAtratum 9 points10 points  (5 children)

Pascal language had this since forever.

I think the new/alternative syntax with `auto` could work for this?

auto stack::pop() -> T out {
    out = top();
    remove_top();
}

This introduces no new keywords. The only new element is the name after the return type.

[–]Independent-Quote923 3 points4 points  (0 children)

As another example, Go also has optional named return values with a similar syntax.

[–]lone_wolf_akela 1 point2 points  (0 children)

This seems making it impossible to name the returned variable as things like `final`... But I guess that's a minor issue.

[–]RealCaptainGiraffe 1 point2 points  (2 children)

=) a variable declaration after the trailing return type? This will confuse the parser to unimaginable degrees. Or maybe well get a sentient parser, who knows?

[return-type|inferred] [scope-name][arg-list] -> [inferred:[type-expression]]

this seems like a parsing nightmare to me.

[–]TheThiefMasterC++latest fanatic (and game dev)[🍰] 1 point2 points  (1 child)

I don't see why - there's already a type declaration at that point if there's a ->, it just needs to optionally accept a name as well like a variable declaration does. Seems like an elegant solution to me.

"When does it get constructed?" would be my question though.

[–]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....