
C++20 Reference Card - cxcl
https://www.bfilipek.com/2020/01/cpp20refcard.html
======
gregsadetsky
Direct links:

C++20 Features
[https://mcusercontent.com/e93417593cbf4da3dba03d672/files/42...](https://mcusercontent.com/e93417593cbf4da3dba03d672/files/4263f191-6c76-43b6-b33b-984c34cdd737/cpp20_refcard_29th_jan.pdf)

Language Features of C++17
[https://gallery.mailchimp.com/e93417593cbf4da3dba03d672/file...](https://gallery.mailchimp.com/e93417593cbf4da3dba03d672/files/ef3a6d15-7f2f-4f5f-8c11-90f2efd2f0e8/cpp17_language.pdf)

~~~
erichdongubler
Thanks. I really don't want another newsletter in my email inbox!

------
pron
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.

~~~
jcelerier
> 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](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.

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

    
    
        template <class T>
        concept SignedIntegral = std::is_integral_v<T> &&
                                std::is_signed_v<T>;
        template <SignedIntegral T> // 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.

~~~
jcelerier
> assert(std.meta.trait.isSignedInt(@TypeOf(val)));

well yes, that's my point. You could also write

    
    
        void signedIntsOnly(auto val) {
            static_assert(std::is_integral_v<T> && std::is_signed_v<T>); 
        }
    

in C++ but it's quite preferable to refactor this inside the types themselves
(for the same reason that you want to refactor any other kind of code in
general - besides you don't want to go read the implementation of your
functions every time do you ? IDEs show us only prototypes for a reason).

How would you for instance specialize a generic function in Zig ? Does zig
accept

    
    
        fn complexmathoperation(val: var) void {
            if(isInt(@TypeOf(val))) { 
            }
        }
    

and in another module

    
    
        fn complexmathoperation(val: var) void {
            if(isSomeKindOfLibrarySpecificMatrix(@TypeOf(val))) { 
            }
        }
    

and properly dispatches between both ?

I'd be hard pressed to say that it is "as powerful" given what I'm seeing for
now.

~~~
pron
> besides you don't want to go read the implementation of your functions every
> time do you ? IDEs show us only prototypes for a reason

The IDE can be changed to show you assertions in addition to the signature,
just as it also shows doc comments. That the signature is special isn't some
law of nature, it's just habit (and users of untyped languages don't even have
those).

> How would you for instance specialize a generic function in Zig ? ... and
> properly dispatches between both ?

No, you'd need a single definition with a comptime branch.

> I'd be hard pressed to say that it is "as powerful" given what I'm seeing
> for now.

What you're seeing is personal taste. In my taste, that Zig doesn't allow
overloading and has clear dispatch is clearer is more preferable (in fact,
it's touted as one of Zig's biggest strengths); in yours, it is _less_
preferable. That's perfectly OK, but it's got nothing to do with expressive
power (which has a pretty good and precise definition:
[https://youtu.be/43XaZEn2aLc](https://youtu.be/43XaZEn2aLc)). There is a
reason why some languages are more complicated while others are simple -- some
people aesthetically prefer the more complex languages while others prefer
simpler ones. What is impressive about Zig is that despite being simple, it's
not giving up power, at least in those aspects I mentioned. C, another simple
language, cannot do those things.

------
wyldfire
> Range-based for with initialiser

Gee, I would really love something like Python's enumerate() and zip() (but
yes, I know that there's alternatives available that are not in the standard
library).

> std::format / Python-like formatting library in the Standard Library!

Yes, this is a good addition.

> Ranges / A radical change in how we work with collections!

... but I still have to use verbose warts like std::iota in order to make a
collection of integers?

EDIT: no, apparently not. Sanity prevails! (see [1]):

    
    
        for (int i : std::views::iota{1, 10})
            std::cout << i << ' ';
    
    

[1]
[https://en.cppreference.com/w/cpp/ranges/iota_view](https://en.cppreference.com/w/cpp/ranges/iota_view)

------
melling
Do people like printed reference cards?

I’m working on a Swift Cookbook.

It started as web pages but I’ve moved it to Github. Considering doing an ePub
or trying to cram a lot into a one pager.

Having Strings, Sets, Arrays, Dictionaries, and Functional examples on one
page might be useful, for example.

Here are my functional examples:

[https://github.com/melling/SwiftCookBook/blob/master/functio...](https://github.com/melling/SwiftCookBook/blob/master/functional.md)

If you know a couple languages, my thinking is that a few pages of examples
should be enough to get started in another language.

~~~
gregsadetsky
Not sure about printed per se, but would definitely appreciate a modern
concise Swift reference!

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

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

~~~
DoofusOfDeath
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.

~~~
otabdeveloper2
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.

~~~
andrekandre
i’m not disagreeing with you (i’m learning rust, so don’t have a strong
opinion yet) but am curious how you feel that way?

------
asdfasgasdgasdg
All this stuff actually made it in!? Concepts look great! Love ranges too.

~~~
forgotpwd16
There is nothing that cannot make it in C++.

