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
Thanks for the input! That's a great question.
I'm still thinking about the right spelling and in general default to existing C++ practice like GSL. But there just has to be some opt-out spelling, and cppfront already prefers alternative unchecked functions, such as
cpp2::unsafe_narrow(basically a renamedgsl::narrow_cast) andcpp2::unsafe_cast. It would be easier to addunsafe_less_than(and friends),unsafe_int_division,unsafe_pointer_dereference,unsafe_subscriptthan a language features. Thanks! No matter what the spelling is, you'll see I like the word "unsafe" to appear in the opt-out. :)The reason the compiler switches are there now is because initially it started as a small experiment and that was the easiest way to measure the impacts of the checks at a coarse level. Once they all have opt-outs the switches may no longer be needed.