I have a function:
static Bwah boo(){
Bwah bwah;
return bwah;
}
And a main function:
int main(){
Bwah boo = Assigner::boo();
cout << "got here.." << endl;
}
The destructor to Bwah is only called once, after the “got here” print.
Is this guaranteed or is this a compiler optimization?
This is an optimization called Return Value Optimization (RVO). It is a common optimization, but you can’t rely on it.
Here are two really excellent links for learning more:
The Wikipedia article in particular directly addresses your question. But the other article goes more in depth about the whole issue.