Live data from Hacker News

C++20 Reference Card

bfilipek.com

11–20 of 48 posts

Re: C++20 Reference Card

#11
post #4

Could someone please explain why you'd put nodiscard on a constructor? I'm a bit confused as to when that's needed.

To avoid this insidious bug:

    std::lock_guard(m);  // guard is immediately thrown away
versus the correct

    std::lock_guard g(m);  // hold mutex til end of scope

Re: C++20 Reference Card

#12
post #6

Earlier quoted context omitted.

So it's for copy/move-constructors only?

No. Sorry for being unclear. [1] is a great video that covers a lot of good stuff in particular this usability bug. It's worth watching in its entirety. If you don't want to sit through the video, here's the broken code in question: void Obj::update() noexcept { unique_lock (m_mutex); do_the_mutation(); } "unique_lock (m_mutex);" has no effect, but you might mistakenly think that this will block on m_mutex. Labeling…

Ahh, that makes sense! On my phone at the moment but will check out the video too, thanks!

Re: C++20 Reference Card

#13
post #4

Could someone please explain why you'd put nodiscard on a constructor? I'm a bit confused as to when that's needed.

To avoid this insidious bug: std::lock_guard (m); // guard is immediately thrown away versus the correct std::lock_guard g(m); // hold mutex til end of scope

Note for people who don’t see the difference: if a guard doesn’t have a name, it won’t survive until the end of the scope, thus doesn’t guard anything.

Re: C++20 Reference Card

#15
Whenever I see new C++ features I'm all the more impressed with how Zig manages to provide capabilities similar to -- and at least as powerful as -- type templates, value templates, constexprs, concepts, and conditional compilation (preprocessor), with a single feature and keyword, without any type/template-level programming, a preprocessor or AST macros (i.e. mini-languages different from the ordinary computation language that one has to learn and can make code quite opaque), all in a language that is about as simple as C and can be fully learned in a day or two.

Re: C++20 Reference Card

#16
post #14
post #4

Could someone please explain why you'd put nodiscard on a constructor? I'm a bit confused as to when that's needed.

After not using C++ for 15+ years , looking at it now makes my eyes bleed.

That's how I felt looking at C++17 deduction guides, and that's even though I've actually been using C++. If you haven't seen them yet you should check them out ;)

Re: C++20 Reference Card

#18
post #13

Earlier quoted context omitted.

To avoid this insidious bug: std::lock_guard (m); // guard is immediately thrown away versus the correct std::lock_guard g(m); // hold mutex til end of scope

Note for people who don’t see the difference: if a guard doesn’t have a name, it won’t survive until the end of the scope, thus doesn’t guard anything.

[deleted]

Re: C++20 Reference Card

#19
post #15

Whenever I see new C++ features I'm all the more impressed with how Zig manages to provide capabilities similar to -- and at least as powerful as -- type templates, value templates, constexprs, concepts, and conditional compilation (preprocessor), with a single feature and keyword, without any type/template-level programming, a preprocessor or AST macros (i.e. mini-languages different from the ordinary computation la…

Could you give or point to some examples?

Re: C++20 Reference Card

#20
post #19
post #15

Whenever I see new C++ features I'm all the more impressed with how Zig manages to provide capabilities similar to -- and at least as powerful as -- type templates, value templates, constexprs, concepts, and conditional compilation (preprocessor), with a single feature and keyword, without any type/template-level programming, a preprocessor or AST macros (i.e. mini-languages different from the ordinary computation la…

Could you give or point to some examples?

https://kristoff.it/blog/what-is-zig-comptime/
Post reply on HN