Could someone please explain why you'd put nodiscard on a constructor? I'm a bit confused as to when that's needed.
std::lock_guard<std::mutex>(m); // guard is immediately thrown away
versus the correct std::lock_guard<std::mutex> g(m); // hold mutex til end of scopeThe same way that int (a); and int a; are equivalent.
So the guard should remain until end of scope, but won't guard anything.
If you don't want to sit through the video, here's the broken code in question:
void Obj::update() noexcept {
unique_lock<mutex>(m_mutex);
do_the_mutation();
}
"unique_lock<mutex>(m_mutex);" has no effect, but you might mistakenly think that this will block on m_mutex. Labeling unique_lock's constructor as [[nodiscard]] would force you to think twice. See [2] for more details.[1] https://youtu.be/lkgszkPnV8g?t=1767
[2] http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2019/p177...
I'm trying to gradually replace C++ with Rust as my primary language, but it's still a lot easier to get a well-paying job in C++ than in Rust.