|
You are here |
rust.godbolt.org | ||
| | | | |
gcc.godbolt.org
|
|
| | | | | template<class T1, class T2> struct MyPair { T1 first; T2 second; static constexpr bool has_references = std::is_reference_v<T1> || std::is_reference_v<T2>; MyPair(const MyPair& other) = default; MyPair(const T1& x, const T2& y) : first(x), second(y) {} MyPair& operator=(const MyPair&) requires(!has_references) = default; MyPair& operator=(const MyPair& other) requires(has_references) { this->~MyPair(); return * new (this) MyPair(other); } }; int main() { int x = 10; int y = 1; MyPair<int&, int> a(x, 5); MyPair<int&, int> b(y, 10); b = a; return b.first; // 10 } | |
| | | | |
gcc.godbolt.org
|
|
| | | | | template<class T1, class T2> struct MyPair { T1 first; T2 second; static constexpr bool has_references = std::is_reference_v<T1> || std::is_reference_v<T2>; MyPair(const T1& x, const T2& y) : first(x), second(y) {} MyPair& operator=(const MyPair&) requires(!has_references) = default; MyPair& operator=(const MyPair& other) requires(has_references) { first = other.first; second = other.second; return *this; } }; int main() { int x = 10; MyPair<int&, int> a(x, 5); MyPair<int&, int> b(x, 10); b = a; } | |
| | | | |
gcc.godbolt.org
|
|
| | | | | // By value because it fits in the register. int extract(const std::pair<int, int> p) { return p.second; } int extract(const std::tuple<int, int> p) { return std::get<1>(p); } std::pair<int, int> make_pair(int a, int b) { return {a, b}; } std::tuple<int, int> make_tuple(int a, int b) { return {a, b}; } | |
| | | | |
msm.runhello.com
|
|
| | | |||