C++20 Reference Card
bfilipek.com
bfilipek.com
C++20 Features https://mcusercontent.com/e93417593cbf4da3dba03d672/files/42...
Language Features of C++17 https://gallery.mailchimp.com/e93417593cbf4da3dba03d672/file...
"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?
With .NET I'd love integration with existing types and generics too.
Actually, I was just thinking aloud. I don't really mean for Zig authors to target .NET, but rather have .NET Core runtime and C# to have the concept of compile-time values and types.
CLR was designed from the start to support C++.
Back in the 2000, "Everything.NET" Microsoft, there was the goal of having everything on top of .NET, it just did not play out that way due to the endless Windows/Office vs .NET politics across MS teams.
Using C++/CLI is much more productive for interop than manually writing P/Invoke declarations.
Or using C++/WinRT to wrap C++ API into COM/UWP libraries.
Likewise on Java, microEJ and Android world, with Android Studio, Eclipse, Netbeans and Oracle Studio you can easily do mixed language development and debugging with C and C++.
Any systems programming language that wants a place at the table on these platforms, needs to cover these workflows as well as C++ does.
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.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.
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.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.
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). 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.
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_viewI’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...
If you know a couple languages, my thinking is that a few pages of examples should be enough to get started in another language.
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...
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.
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.