Skip to content

Instantly share code, notes, and snippets.

@AndreLouisCaron
Created September 5, 2012 17:04
Show Gist options
  • Save AndreLouisCaron/3640062 to your computer and use it in GitHub Desktop.
Save AndreLouisCaron/3640062 to your computer and use it in GitHub Desktop.
std::vector<int> multiply (std::vector<int> input, int value)
{
for (auto& i : input )
i *= value;
return input;
}
int main (int, char**)
{
std::vector<int> foo;
foo.push_back(1);
foo.push_back(2);
foo = multiply(foo, 2);
// Assuming a (very) aggressive inlining compiler,
// the last line could be rewritten as:
for (auto& i : foo)
i *= 2;
// ... no copy or move occurs!
}
@AndreLouisCaron
Copy link
Author

Perhaps I'm pushing it, but does is this inlining technically legal, given the standard "explicitly forbids eliding the move in [the case that the function returns its argument]"?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment