Created
November 17, 2018 20:13
-
-
Save bathoryr/241bd98a00c12069dc68effa73cf8fdc to your computer and use it in GitHub Desktop.
Rvalue references
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
| // How to convert lvalue to rvalue, because the compiler treats a named rvalue reference as an lvalue | |
| // and an unnamed rvalue reference as an rvalue | |
| // https://docs.microsoft.com/en-us/cpp/cpp/rvalue-reference-declarator-amp-amp?view=vs-2017#additional-properties-of-rvalue-references | |
| std::vector<std::thread> thread_list; | |
| for (int i = 0; i < 5; i++) | |
| { | |
| std::thread t(thread_proc, dist(engine)); | |
| // convert to rvalue explicitly | |
| thread_list.push_back(static_cast<std::thread&&>(t)); | |
| // or create rvalue by default - temporary object cannot be referenced elsewhere | |
| thread_list.push_back(std::thread(thread_proc, dist(engine))); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment