by einpoklum · 6y ago · 0 comments
* Textual inclusion: C++20 has modules, where you include sematically, not textually.
* Bitwise operator precedence: Not soundly supported, but you can sort-of have custom infix operators in C++, see: https://stackoverflow.com/questions/15632217/defining-new-in... so, specifically, one could implement the alternative modulo.
* Leading zero for octal: C++ has custom literals, so you could define a _octal or _hex etc and get the value determined at compile-time.
* No power operator: Same thing in Odin and C and C++, IIANM.
* Switch with default fallthrough: Since C++17, there's an official [[fallthrough]] anottation. You can make your compiler fail in cases when you implicitly fallthrough. So, not as elegant, but you can ensure you don't mess up and get the wrong behavior.
* (Type syntax: Nope, C++ has the contrived syntax of C.)
* Weak typing: With a library, you can have strong aliases which are not interchangeable with the aliased type. See this blog post: https://foonathan.net/2016/10/strong-typedefs/ and the library it links to. Things would be better, though, if the C++ standard committee allowed for an operator.
* Bytestrings: This is a library issue really. And both C and C++ have libraries which deal with wider characters, with UTF-8 and what-not.
* ++ and -- : You don't _have_ to use them... and it's possible to have static tools which forbid them in source files.
* ! Operator: C++ has !, && and || , but also "not", "and" and "or". I like the latter.
* Multiple returns: It's easy to return a tuple in C++, and with C++17 you can even construct and initialize multiple variables like that, e.g. `auto [index, name] = get_index_and_name(whatever)`.
* Errors: C++ is multi-paradigmatic here, supporting the traditional status return, an expected<T> type (either an actual T or an error; not yet standardized by available via widely-used ibrary), and exceptions. Monadic-style programming is not yet supported and will likely not be in C++20, but there are discussions about this.
* Nulls: With `std::optional` and `gsl::non_null`, and especially with no return types necessary, you can comfortably avoid using `nullptr` in C++ code.
So - with some choices and a little discipline (really not much!), you can have all of this "Odinish" behavior you like.
Of course - the price of C backwards compatibility and supporting multiple programming paradigms is complexity of the formal language definition, grammatic ambiguity, and a syntax that is not always pleasant.
... provided that the libraries that you want to use are available as C++20 modules, not header files.
> * Leading zero for octal: C++ has custom literals,
... but no way to redefine the built-in "0" prefixed octal literal.
> * Weak typing: With a library, you can have strong aliases which are not interchangeable with the aliased type.
... you can have that in theory, I agree it's very useful, but can you name one widely used C++ library that e.g. defines custom integer or string types purely for the purpose of type safety?
> * Bytestrings: This is a library issue really.
... indeed, and the standard library's std::string is quite unhelpful, there's no way provided to iterate code points if you put UTF-8 in std::string.
> * Errors: C++ is multi-paradigmatic here,
... but none of the paradigms available with just the language and standard library have any static checks that the caller of a function actually handles an error, whether return value or exception.
> * Nulls: With `std::optional` and `gsl::non_null`, and especially with no return types necessary, you can comfortably avoid using `nullptr` in C++ code.
... provided you never use any library from the C++ ecosystem, none of which currently use std::optional or gsl::non_null.
One of the problems I have with C++ is that re-using existing libraries vs. structuring your code to statically avoid sources of bugs is a trade-off; it shouldn't be.
Yes. This will take time. But if you're writing most of the world from scratch (like with a new programming language), then you can write libraries in modules.
> ... but no way to redefine the built-in "0" prefixed octal literal.
True, but why use that? It's confusing if you don't know the convention.
> ... can you name one widely used C++ library that e.g. defines custom integer or string types purely for the purpose of type safety?
Well, custom string types? QString is used in tons of apps; but custom string types are rarely about safety. Integers - there's Boost's safe_numerics library. Granted, I'm not sure it's very popular or just mildly popular, but still.
> ... indeed, and the standard library's std::string is quite unhelpful, there's no way provided to iterate code points if you put UTF-8 in std::string.
Yes, Unicode support in the C++ standard library is lacking, or where it isn't, it might as well have been. But again - it's not the language. So you can write your string library (or port ICU) in Odin or in C++
> ... but none of the paradigms available with just the language and standard library have any static checks that the caller of a function actually handles an error, whether return value or exception.
static checks are a compiler/IDE thing. But - there _is_ a [[nodiscard]] annotation in C++17, so that you can't just ignore a returned status altogether.
> ... provided you never use any library from the C++ ecosystem, none of which currently use std::optional or gsl::non_null.
Toucheel and the backwards compatibility is a bitch here. But
1. At those libraries' boundary, you have either silent or explicit conversion, which are safe, as long as you act safely on the outside. 2. Most templated standard library code will use optional just fine...
> One of the problems I have with C++ is that re-using existing libraries vs. structuring your code to statically avoid sources of bugs is a trade-off; it shouldn't be.
Agreed. Having said that - writing lightweight wrappers for an existing library is a middle-of-the-road solution which is a lot less bug prone than just writing unsafe, old-style C++ all over.
1. Library use boundaries are indeed a place where you often need to "unpack" more complex types. But then - external library APIs do tell you whether they take ownership of pointers and generally what you can expect from them. And it's not so bad to write
lib_foo_function(my_unique_ptr.release(), other, args);
instead of
my_function(std::move(my_unique_ptr), other, args);
2. Libraries slowly adopt features of language standards, and typically lag behind. To be honest, though - this is going slower than I would have liked or expected.
References for non-null parameters, RAII, library types with bounds checking, primitive smart pointers, ...