Hacker News new | ask | show | jobs
by recursivecaveat 205 days ago
It's okay to admit when C++ doesn't have a feature. std::variant is an approximation of sum type support. It's ergonomics are an absolute travesty as a result, any errors you get are going to be 5 pages of template gunk, and I'm sure that using it pervasively is terrible for compile times. It has been possible to construct a type safe unit library as demonstrated in the article for forever. I've never seen anyone use this ability in a production codebase, because a terrible library emulation of a feature is not a real feature.

The notion that just using the new fancy types automatically makes everything memory safe has to stop. std::expected contains either a value or an error. If you call .value() and you're wrong you get an exception. If you call .error() and you're wrong you get undefined behaviour. This was added in C++23. Since there's no destructuring you have to call these methods btw, just don't make any mistakes with your preconditions! Regardless 90% of memory safety errors I see are temporal. Unless we completely ban references and iterators they will not be going anywhere. Using unique_ptr instead of new does not do anything when you insert into a map while holding a reference to an element.

Developers also have to be able to make their own things. We can't pretend that absolutely everything we will ever need is bundled up in some perfect library. To write a typesafe application you need to be able to create your own domain specific abstractions, which to me precludes them looking like this:

    template <class ty>
    concept db_result_tuple = requires { typename remove_cvref_t<ty>; }
                           && []<class... Es>(std::tuple<Es...>*) {
                                return all_result_args_ok_v<Es...>;
      }( static_cast<typename std::add_pointer_t<remove_cvref_t<ty>>>(nullptr) );
2 comments

Well, it is bad to have error() do UB.

It was slightly improved in C++26 under hardening:

https://en.cppreference.com/w/cpp/utility/expected/error.htm...

> If you call .error() and you're wrong you get undefined behaviour. This was added in C++23.

And it was fixed in c++26