When can't a compiler use RVO or NRVO?
c++, move
Solution
The answer is that it is compiler and situation dependent. E.g. control flow branching might confuse optimizers. Wikipedia give this example:
#include <string>
std::string f(bool cond = false) {
std::string first("first");
std::string second("second");
// the function may return one of two named objects
// depending on its argument. RVO might not be applied
return cond ? first : second;
}
int main() {
std::string result = f();
}
Problem
Move semantics can be useful when the compiler cannot use RVO and NRVO. But in which case can't the compiler use these features?