11

Consider the function

const std::string f()
{
    return "hello";
}

And the call

std::string x = f();

Regardless of whether value return types should be const or not, does the fact the return value is const, prevent a compiler from performing return value optimization?

My understanding of RVO is that the returned object is constructed directly into the variable outside the function. However, if the return type is const T, this isn't the same as a T, so will RVO be prevented?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Neil Kirk
  • 21,327
  • 9
  • 53
  • 91
  • I know you said 'regardless whether [...] const or not' the answers in the linked question (and others) do go over this specific question in modern times and old times. – Rapptz Aug 18 '14 at 10:07
  • 2
    @Rapptz I don't think this is a duplicate. The question you found answers, quite correctly, that nowadays, a function shouldn't be returning `const std::string`. This question isn't whether you should do that, but what specifically happens if you do do that. It's a related question, but not a duplicate IMO. –  Aug 18 '14 at 10:10
  • @hvd I don't believe nothing of value will be added by answers to this question since the linked one answers the ultimate state of affairs, i.e. it forbids move semantics and also forbids RVO because a copy has to be made. – Rapptz Aug 18 '14 at 10:12
  • @Rapptz You are saying the linked question has answers stating RVO is forbidden in this case? – Neil Kirk Aug 18 '14 at 10:13
  • @NeilKirk if it does, it stealthy, as RVO is nowhere on the page. – WhozCraig Aug 18 '14 at 10:13
  • @NeilKirk It's implied because you can't drop the `const` when returning, a copy has to be made so RVO doesn't (shouldn't?) take place. The answers do gloss over it, albeit not mentioned explicitly. Like I said, this isn't the first question on the topic. I just picked the first decent looking one. – Rapptz Aug 18 '14 at 10:14
  • The reason why I'm asking is I have legacy code that does this, and the code has shown performance bottlenecks in the past. I want to know whether it's worth the bureaucracy of changing the code or leaving it as it is. – Neil Kirk Aug 18 '14 at 10:15
  • 1
    @Rapptz In other words, the linked questions don't answer my question. – Neil Kirk Aug 18 '14 at 10:15
  • @NeilKirk http://stackoverflow.com/questions/795674/which-are-the-implications-of-return-a-value-as-constant-reference-and-constant http://stackoverflow.com/questions/12051012/should-i-return-const-objects http://stackoverflow.com/questions/14429988/can-a-c-compiler-perform-rvo-for-a-const-return-value http://stackoverflow.com/questions/18435820/const-value-and-rvo – Rapptz Aug 18 '14 at 10:22
  • @Rapptz Ok thanks, the last two answer my question. – Neil Kirk Aug 18 '14 at 10:28
  • @NeilKirk Based on your last comment, I've voted to close as duplicate of http://stackoverflow.com/questions/14429988/can-a-c-compiler-perform-rvo-for-a-const-return-value. That seems really a perfect duplicate. – jogojapan Aug 18 '14 at 10:30
  • @NeilKirk [And observation says it indeed is a duplicate.](http://coliru.stacked-crooked.com/a/d254f98e85c9bf72). It would seem to hold accurate. – WhozCraig Aug 18 '14 at 10:31
  • @NeilKirk Yeah no problem. Sorry 'bout the wrong dupe vote. – Rapptz Aug 18 '14 at 10:32
  • @Rapptz No probs mate. – Neil Kirk Aug 18 '14 at 10:46

0 Answers0