Skip to content

Instantly share code, notes, and snippets.

@bathoryr
Created November 17, 2018 20:13
Show Gist options
  • Select an option

  • Save bathoryr/241bd98a00c12069dc68effa73cf8fdc to your computer and use it in GitHub Desktop.

Select an option

Save bathoryr/241bd98a00c12069dc68effa73cf8fdc to your computer and use it in GitHub Desktop.
Rvalue references
// 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