|
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 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
|
|
| | | | | int main() { return std::move(0); } | |
| | | | |
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 } | |
| | | | |
andreabergia.com
|
|
| | | I usually write parsers by starting from a grammar and either coding a lexer/parser by hand or relying on tools such as the fantastic Antlr. However, a friend recently introduced me to parser combinators, which I found to be very interesting and useful. It's not a recent idea, but it was new to me, and I have found it to be very interesting and useful. | ||