Created
February 20, 2018 16:36
-
-
Save parsa/b5d4c6ba3884180805c0770208eeae15 to your computer and use it in GitHub Desktop.
Move Semantics Demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <utility> | |
| struct A {}; | |
| struct B {}; | |
| void f(B&& b) {} // rvalue ref | |
| void f(B const& b) {} // const lvalue ref | |
| void f1(B b) {} | |
| template <typename T> | |
| void f2(T&& t) { } | |
| template <typename T> | |
| void f3(T&& t) | |
| { | |
| f2(std::forward<T>(t)); | |
| } | |
| int main() | |
| { | |
| { | |
| B b; | |
| f(b); | |
| f(std::move(b)); | |
| } | |
| { | |
| B b; | |
| f1(b); | |
| f1(std::move(b)); | |
| } | |
| { | |
| B b; | |
| f3(b); | |
| f3(std::move(b)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment