Skip to content

Instantly share code, notes, and snippets.

@nthery
Created April 27, 2023 08:13
Show Gist options
  • Save nthery/26b4e87408a3738d6cc7ec9ce57669dd to your computer and use it in GitHub Desktop.
Save nthery/26b4e87408a3738d6cc7ec9ce57669dd to your computer and use it in GitHub Desktop.
// Example showing when temporary materialisation and copy elision occurs.
struct S {
S();
~S();
S(const S&);
S& operator=(const S&);
S(S&&) noexcept;
S& operator=(S&&) noexcept;
S& set(int);
};
void h();
void f() {
// initialisation causes temporary materialisation => copy elision
auto s = S();
h();
}
void g() {
// calling set() causes temporary materialisation => copy not elided
auto s = S().set(42);
h();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment