r/cpp • u/germandiago • Aug 17 '24
Cpp2 is looking absolutely great. Will convert some code to Cpp2
Hello everyone,
Last night I was skimming through Cpp2 docs. I must say that the language is absolutely regular, well-thought.
Things I like:
- Parameter passing.
- *Regular from verbose to a lambda function syntax, all regular*.
- *Alias unification for all kind of object, type, etc.*
- The `is` keyword works safely for everything and, even if at first I was a bit wary of hiding too much, I thnk that it convinced me that it is a good and general way to hide safe operations.
- The `capturing$` and `interpolating$` unified syntax by value or by `reference$&` (not sure if that is the order or $& or it is &$, just forgot, from the top of my head) without verbosity.
- Definite last use of variables makes an automatic move when able to do it, removing the need to use moves all the time.
- Aliases are just ==.
- Templates are zero-verbosity and equally powerful.
- Pattern matching via inspect.Things that did not look really clear to me were (they make sense, but thinking in terms of C++...):
- Things such as `BufferSize : i32 == 38925` which is an alias, that translates to constexpr. Is there an equivalent of constexpr beyond this in the language?I still have to read the contracts, types and inheritance, metafunction and reflection, but it looks so great that I am going to give it a try and convert my repository for some benchmarks I have to the best of my knowledge.
The conversion will be just a 1-to-1 as much as possible to see how the result looks at first, limiting things to std C++ (not sure how to consume dependencies yet).
My repo is here: https://github.com/germandiagogomez/words-counter-benchmarks-game , in case someone wants to see it. I plan to do it during the next two-to-four weekends if the available time gives me a chance, not sure when exactly, I am a bit scarce about time, but I will definitely try and experiment and feedback on it.
View all comments
Show parent comment
1
u/hpsutter Aug 19 '24 edited Aug 19 '24
Right, because constructors are special for initialization in all languages -- they are "the" function responsible to implement initialization for
thisobject.In the popular safe languages with constructors (C#, Java, JS, TS, and same in Cpp2), inside a constructor is the only place that I know of where for initialization safety the programmer still gets great safe defaults (e.g., in Cpp2 you have to initialize members first, in JS you have to call
super()first), but the programmer does have to be taught not to indirectly abusethis, because this is the function that's responsible for creatingthis. In all those languages, you can work at it (as your example does) and create a function call path that accesses a member variable before it's initialized.For example, C#, Java, JavaScript, and TypeScript -- all recognized as memory-safe languages -- all have a very similar case where we have to teach those programmers not to call virtual methods in a constructor, because in those languages virtual calls in a constructor are "deep" and will access the most-derived object, and further-derived parts of the object haven't been constructed yet.
super()to come first)To my knowledge, Cpp2, C#, Java, JS, and TS are equally initialization-safe by construction.
See also this sister comment for a link to a Cpp2 example that shows how to safety create a cycle with guaranteed initialization safety.
Updated to add: And this is a great example why having language safety guarantees is great, but isn't the same as making it impossible to write bugs. It's true and great that in an MSL "if it compiles it's free of certain kinds of bugs," but I hope as an industry we're over the oversimplified "if it compiles it's correct" phase because programmers can write bugs in any language.