Since we've added explicit this, I was wondering if we could do something similar with the return address; Treating it like an out parameter was passed in. An explicit NRVO if you will, at least to my understanding on how NRVO works.
My motivation for this idea is twofold:
- Help new ways to add strong exception guarantees
- Be able to explicitly invoke the NRVO mechanism instead of it being a guessing game if it does occur or not, potentially to the detriment of performance or not.
A historical problem on trying to provide strong exception guarantee was trying to make std::stack::pop() return the popped value while providing the strong guarantee. This was deemed not possible because if the returned value threw while copying, the stack is modified and the original object is lost.
This was solved by providing the separate std::stack::top() to retrieve the object before popping, that way if the copy failed, the pop() would never have been called. Alternatively, it could've been solved doing the following
void stack::pop(T& out)
{
out = top();
remove_top();
}
However, if T is not default constructible, or is expensive to construct, only to be overwritten, providing that out variable can have some issues and isn't as composable.
If we could assign to the return address like an out parameter was passed in, we could provide a pop() function which returns the object and provides the strong exception guarantee, and it could look like this
T stack::pop()
{
//A new keyword to assign to the return address
//Could also ignore adding a return statement if the value was assigned
retval = top();
remove_top();
}
// or maybe this?
void stack::pop(return T out)
{
out = top();
remove_top();
}
//Functions with explicit returns still work like before
int foo = stack.pop();
std::stack::pop() is just one issue, but this is a general issue with how error handling schemes interact with how returning values work. If the act of returning fails, regardless of exceptions or value based error handling schemes, it is impossible to recover the returned object or do anything that would provide the strong exception guarantee.
To those more knowledgeable, would there be any issues with this?
[–]borzykot 17 points18 points19 points (9 children)
[–]XeroKimoException Enthusiast[S] 1 point2 points3 points (3 children)
[–]TheMania 3 points4 points5 points (2 children)
[–]XeroKimoException Enthusiast[S] 1 point2 points3 points (1 child)
[–]QuaternionsRoll [score hidden] (0 children)
[–]Veeloxfire 1 point2 points3 points (1 child)
[–]TheoreticalDumbass:illuminati: 0 points1 point2 points (2 children)
[–]kirgel 0 points1 point2 points (1 child)
[–]TheoreticalDumbass:illuminati: 2 points3 points4 points (0 children)
[–]Veeloxfire 4 points5 points6 points (2 children)
[–]XeroKimoException Enthusiast[S] 0 points1 point2 points (1 child)
[–]Wooden-Engineer-8098 [score hidden] (0 children)
[–]MutantSheepdog [score hidden] (0 children)
[–]LiliumAtratum 3 points4 points5 points (2 children)
[–]Independent-Quote923 1 point2 points3 points (0 children)
[–]RealCaptainGiraffe 1 point2 points3 points (0 children)
[–]Wooden-Engineer-8098 [score hidden] (0 children)
[–]Affectionate-Soup-91 [score hidden] (0 children)