Live data from Hacker News

C++20 Reference Card

bfilipek.com

31–40 of 48 posts

Re: C++20 Reference Card

#31
post #28

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

I might be wrong here, but I think that the first one is a declaration of a guard with the name m using the default constructor. The same way that int (a); and int a; are equivalent. So the guard should remain until end of scope, but won't guard anything.

lock_guard doesn’t have a default ctor, you have to pass a mutex (or presumably invoke a move ctor, but I can’t see one on cppref).

Re: C++20 Reference Card

#32
post #28

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

I might be wrong here, but I think that the first one is a declaration of a guard with the name m using the default constructor. The same way that int (a); and int a; are equivalent. So the guard should remain until end of scope, but won't guard anything.

Lock guard has no default constructor, so the line will in fact materialize a temporary lock guard around the mutex m, then throw it away at the semi-colon, thus locking nothing.

Re: C++20 Reference Card

#33
post #26
post #20

Earlier quoted context omitted.

https://kristoff.it/blog/what-is-zig-comptime/

Damn it looks good. I wish it could target and consume .NET or Java.

What does that even mean?

"Target" I guess could mean that it could compile to code for those virtual machines? That would make some sense although for a language that sets out to compete with/replace C it would be a bit strange.

For "consume" I don't know ... do you mean "call into", or something?

Re: C++20 Reference Card

#34
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

#35
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…

> 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

are you sure ? zig does not seem to have something that looks like concepts (an issue is open : https://github.com/ziglang/zig/issues/1268) nor a way to put restrictions on a given type passed to a function or series of function.

e.g. it seems that what in C++ would be (with concepts, but the same can be written more verbosely with C++03 templates if needed)

    void f(IsFoobar auto x) {
     
    }
    void g(IsFoobar auto y) {
     
    }
would require in zig to have a comptime test for the "foobar" nature of T in every function, which looks like a downgrade and does not expose the requirements cleanly in the function interface.

Re: C++20 Reference Card

#36
post #14

Earlier quoted context omitted.

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

I use it everyday, and I'm still very eager to stop using it due to its growing complexity. 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.

Rust is both more complex and uglier than modern C++. If your complaint is having to learn new stuff, then Rust isn't the solution.

Re: C++20 Reference Card

#37
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…

> 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 are you sure ? zig does not seem to have something that looks like concepts (an issue is open : https://github.com/ziglang/zig/issues/1268 ) nor a way to put restrictions on a given type passed to a function or series of functi…

> zig does not seem to have something that looks like concepts ... nor a way to put restrictions on a given type passed to a function or series of function.

Of course it does. Just place a comptime assertion on the @TypeOf a var.

> would require in zig to have a comptime test for the "foobar" nature of T in every function, which looks like a downgrade and does not expose the requirements cleanly in the function interface.

Cleanliness is a matter of personal aesthetic preference, and a rich type specification "in every function" is not less work or less clear than a comptime assertion in every function. In either case you get a clear compilation error. Requirements are never fully specified in the function's interface unless you have dependent types or contracts (both with their own problems and tradeoffs). Even Haskell has partial subroutines. What portions of the spec that is already enforced at compile type should be exposed in the signature -- keeping in mind that it will never be all of them -- is a matter of taste. Zig might shift things around a bit, but the capability to enforce a specification that is at least as strong as structural types at compile-time is already there.

Re: C++20 Reference Card

#38
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…

> 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 are you sure ? zig does not seem to have something that looks like concepts (an issue is open : https://github.com/ziglang/zig/issues/1268 ) nor a way to put restrictions on a given type passed to a function or series of functi…

Here's the C++ example from the article:

    template 
    concept SignedIntegral = std::is_integral_v &&
                            std::is_signed_v;
    template  // no SFINAE here!
    void signedIntsOnly(T val) { }
Here's the equivalent Zig code. You can run this with `zig test example.zig`.

    const std = @import("std");
    const assert = std.debug.assert;

    fn signedIntsOnly(val: var) void {
        comptime assert(std.meta.trait.isSignedInt(@TypeOf(val)));
    }

    test "concepts example" {
        var x: u32 = 1234;
        signedIntsOnly(x);
    }
This produces the compile error:

    ../lib/std/debug.zig:221:14: error: unable to evaluate constant expression
        if (!ok) unreachable; // assertion failure
                 ^
    ./example.zig:5:20: note: called from here
        comptime assert(std.meta.trait.isSignedInt(@TypeOf(val)));
                       ^
    ./example.zig:10:19: note: called from here
        signedIntsOnly(x);
                      ^
    ./example.zig:8:26: note: called from here
    test "concepts example" {
                            ^
    
It accomplishes the same thing, just a bit more verbose, since it is using the general tools of the language rather than special syntax.

Re: C++20 Reference Card

#39
post #22
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…

Then again, it lacks the eco-system, which is what makes me reach out to C++ when going outside Java/.NET.

Sure. Zig isn't even production-ready yet. That doesn't mean that its ability to do so much with such a minimal design isn't very impressive.

Re: C++20 Reference Card

#40
post #39
post #22

Earlier quoted context omitted.

Then again, it lacks the eco-system, which is what makes me reach out to C++ when going outside Java/.NET.

Sure. Zig isn't even production-ready yet. That doesn't mean that its ability to do so much with such a minimal design isn't very impressive.

Agreed, looking into other eco-systems is always a learning experience.
Post reply on HN