为什么标准没有强制要求使用RVO和NRVO优化(当它们适用)?例如,一个函数通常会生成一些对象并将其作为结果返回。由于RVO/NRVO,复制/移动构造函数通常会被省略,但是它们仍然需要被定义,这有点令人困惑。如果RVO/NRVO被纳入标准,那么在这种情况下就不再需要复制/移动构造函数了。
为什么标准没有强制要求使用RVO和NRVO优化(当它们适用)?例如,一个函数通常会生成一些对象并将其作为结果返回。由于RVO/NRVO,复制/移动构造函数通常会被省略,但是它们仍然需要被定义,这有点令人困惑。如果RVO/NRVO被纳入标准,那么在这种情况下就不再需要复制/移动构造函数了。
std::string Func()
{
return std::string("foo");
}
std::string Func()
{
std::string named("foo");
return named;
}
To enforce copy elision in all applicable cases, no matter how difficult to implement for compilers. So now every compiler writer has to deal with the cases like this:
std::string Func(bool b)
{
if(b)
{
std::string named("foo");
return named;
}
else
{
std::string named("bar");
return named;
}
}
Many compilers don't handle NRVO in those cases. And that's a simple case; they can get much more complex than that.
Go through every compiler and find a common subset of cases where copy elision is always used, then specify them in the standard as requirements. That's utterly ludicrous; you'd be standardizing based on implementation details. That's never a good thing.