Skip to content

Instantly share code, notes, and snippets.

@vietjtnguyen
Created February 1, 2018 03:53
Show Gist options
  • Save vietjtnguyen/a42988cbc34be15ba293cd69a376f311 to your computer and use it in GitHub Desktop.
Save vietjtnguyen/a42988cbc34be15ba293cd69a376f311 to your computer and use it in GitHub Desktop.
// https://www.youtube.com/watch?v=2gjroKLyWKE
#include <utility>
struct S
{
// trailing & means only applies to lvalues on left side
S& operator=(const S& s) & = default;
S& operator=(S& s) & = default;
S& operator=(S&& s) & = default; // move from
S& operator=(const S&& s) & = delete; // can't move from const rvalue, no default
// trailing && means only applies to rvalues on left side
S& operator=(const S& s) && = delete; // nonsensical to assign to temp (int can't)
S& operator=(S& s) && = delete; // nonsensical to assign to temp (int can't)
S& operator=(S&& s) && = delete; // (i) nonsensical to assign to temp (int can't)
S& operator=(const S&& s) && = delete; // can't move from const rvalue, no default
// const versions, but assigning to const makes no sense, no defaults
// trailing & means only applies to lvalues on left side
S& operator=(const S& s) const & = delete;
S& operator=(S& s) const & = delete;
S& operator=(S&& s) const & = delete;
S& operator=(const S&& s) const & = delete; // can't move from const
// const versions, but assigning to const makes no sense, no defaults
// trailing && means only applies to rvalues on left side
S& operator=(const S& s) const && = delete;
S& operator=(S& s) const && = delete;
S& operator=(S&& s) const && = delete;
S& operator=(const S&& s) const && = delete; // can't move from const
};
int main()
{
//S{} = S{}; // works if (i) is defaulted
S s{};
S s2{};
s = std::move(s2);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment