This is the
talk page for discussing improvements to the
Return value optimization redirect. This is not a forum for general discussion of the article's subject. |
Article policies
|
Find sources: Google ( books · news · scholar · free images · WP refs) · FENS · JSTOR · TWL |
![]() | This redirect does not require a rating on Wikipedia's
content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||||
|
![]() | A fact from Return value optimization appeared on Wikipedia's
Main Page in the
Did you know column on 28 March 2009 (
check views). The text of the entry was as follows:
| ![]() |
![]() | The contents of the Return value optimization page were merged into Copy elision on 11 November 2017 and it now redirects there. For the contribution history and old versions of the merged article please see its history. |
Wouldn't this be an assignment and not a copy? Thus, calling an assignment operator once and a copy later -- without the optimization. — Preceding unsigned comment added by Zerooneinfinity ( talk • contribs) 06:24, 23 November 2011 (UTC)
void f(Data * _hiddenAddress) {
Data result = {};
// generate result
memcpy(_hiddenAddress, &result, sizeof(Data)); // copy result into hidden object
return;
}
int main() {
Data d;
Data _hidden; // create hidden object
f(&_hidden);
memcpy(&d, &_hidden, sizeof(Data)); // copy the result into d
}
Apart from requiring the existence of a copy constructor, copy initialization also requires an implicit constructor of the correct type. Direct initialization however allows the compiler either to use an explicit constructor of the correct type, or implicitly convert the provided argument into an intermediate type. -- 80.175.250.222 ( talk) 00:08, 18 January 2010 (UTC)
I think this article needs to be split apart. The segment on "Other forms of copy elision" has absolutely nothing to do with Return Value Optimization, other than the fact that both are forms of copy elision. I'm going to do the split in one week if nobody says otherwise. —Preceding unsigned comment added by Billyoneal ( talk • contribs) 05:34, 11 April 2010 (UTC)
decltype
(
talk)
21:08, 17 April 2010 (UTC)
It may be meaningful to mention move constructors in C++11, as they fix the problem noted in the last section, where the compiler may be unable to perform the optimization. — Preceding unsigned comment added by 68.173.69.69 ( talk) 21:56, 2 March 2012 (UTC)
Example shown will indeed certainly copy the string.
However, how could it be different? If this wasn't a function (i.e. same code copy/pasted directly in the main), two intermediate objects would have been created anyway, and a copy will happen anyway... (except if there are deeper/other compiler optimization).
So IMHO the example is wrong, because it's misleading.
It should be something like this (for the f part):
std::string f(bool cond = false) {
return cond ? std::string first("first") : std::string second("second");
}
In this case, the RVO optimization is more likely to be applied.
I suggest this article should be merged with the Copy elision article, they both talk about the same thing. Return value optimization could be a section of the Copy elision article. -- Living001 ( talk) 07:36, 12 July 2013 (UTC)
This is the
talk page for discussing improvements to the
Return value optimization redirect. This is not a forum for general discussion of the article's subject. |
Article policies
|
Find sources: Google ( books · news · scholar · free images · WP refs) · FENS · JSTOR · TWL |
![]() | This redirect does not require a rating on Wikipedia's
content assessment scale. It is of interest to the following WikiProjects: | ||||||||||||||||||||
|
![]() | A fact from Return value optimization appeared on Wikipedia's
Main Page in the
Did you know column on 28 March 2009 (
check views). The text of the entry was as follows:
| ![]() |
![]() | The contents of the Return value optimization page were merged into Copy elision on 11 November 2017 and it now redirects there. For the contribution history and old versions of the merged article please see its history. |
Wouldn't this be an assignment and not a copy? Thus, calling an assignment operator once and a copy later -- without the optimization. — Preceding unsigned comment added by Zerooneinfinity ( talk • contribs) 06:24, 23 November 2011 (UTC)
void f(Data * _hiddenAddress) {
Data result = {};
// generate result
memcpy(_hiddenAddress, &result, sizeof(Data)); // copy result into hidden object
return;
}
int main() {
Data d;
Data _hidden; // create hidden object
f(&_hidden);
memcpy(&d, &_hidden, sizeof(Data)); // copy the result into d
}
Apart from requiring the existence of a copy constructor, copy initialization also requires an implicit constructor of the correct type. Direct initialization however allows the compiler either to use an explicit constructor of the correct type, or implicitly convert the provided argument into an intermediate type. -- 80.175.250.222 ( talk) 00:08, 18 January 2010 (UTC)
I think this article needs to be split apart. The segment on "Other forms of copy elision" has absolutely nothing to do with Return Value Optimization, other than the fact that both are forms of copy elision. I'm going to do the split in one week if nobody says otherwise. —Preceding unsigned comment added by Billyoneal ( talk • contribs) 05:34, 11 April 2010 (UTC)
decltype
(
talk)
21:08, 17 April 2010 (UTC)
It may be meaningful to mention move constructors in C++11, as they fix the problem noted in the last section, where the compiler may be unable to perform the optimization. — Preceding unsigned comment added by 68.173.69.69 ( talk) 21:56, 2 March 2012 (UTC)
Example shown will indeed certainly copy the string.
However, how could it be different? If this wasn't a function (i.e. same code copy/pasted directly in the main), two intermediate objects would have been created anyway, and a copy will happen anyway... (except if there are deeper/other compiler optimization).
So IMHO the example is wrong, because it's misleading.
It should be something like this (for the f part):
std::string f(bool cond = false) {
return cond ? std::string first("first") : std::string second("second");
}
In this case, the RVO optimization is more likely to be applied.
I suggest this article should be merged with the Copy elision article, they both talk about the same thing. Return value optimization could be a section of the Copy elision article. -- Living001 ( talk) 07:36, 12 July 2013 (UTC)