Why is std::filesystem::u8path deprecated in c++20?

Because, thanks to the existence of the C++20 feature char8_t, this will work: path p(u8″A/utf8/path”); u8path existed to allow the detection of the difference between a UTF-8 string and a narrow character string. But since C++20 will give us an actual type for that, it is no longer necessary. What should I use in c++17? … Read more

Why does C++’s async/await not need an event loop?

Python and JavaScript don’t do CPU threads (well, Python can do threads, but that’s not relevant here, because Python’s thread stuff isn’t inherently await-able). So they fake threading with “event loops”. C++ is a low-level language that knows what CPU threads are and expects to make use of them. As such, when you co_await on … Read more

How to use c++20 modules with GCC?

GCC’s language status page says it doesn’t support modules yet. C++20 support is not complete (which is fair enough given that we’re in 2020! And C++20 technically doesn’t exist yet…). However, with some flags and a development branch you can play around with the in-progress implementation — read more about it on GCC’s Modules Wiki. … Read more

Why was std::bit_cast added, if reinterpret_cast could do the same?

Well, there is one obvious reason: because it wouldn’t do everything that bit_cast does. Even in the C++20 world where we can allocate memory at compile time, reinterpret_cast is forbidden in constexpr functions. One of the explicit goals of bit_cast is to be able to do these sorts of things at compile-time: Furthermore, it is … Read more

How to convert an enum to a string in modern C++

Magic Enum header-only library provides static reflection for enums (to string, from string, iteration) for C++17. #include <magic_enum.hpp> enum Color { RED = 2, BLUE = 4, GREEN = 8 }; Color color = Color::RED; auto color_name = magic_enum::enum_name(color); // color_name -> “RED” std::string color_name{“GREEN”}; auto color = magic_enum::enum_cast<Color>(color_name) if (color.has_value()) { // color.value() -> … Read more

What is C++20’s string literal operator template?

There were two separate proposals: Allowing string literals as non-type template parameters (P0424) Allowing class types as non-type template parameters (P0732) The first proposal was partially merged into the second. String literals still are not valid arguments as non-type template parameters, but they are valid arguments into class types. The example from [temp.arg.nontype]/4 might help: … Read more

how std::u8string will be different from std::string?

Since the difference between u8string and string is that one is templated on char8_t and the other on char, the real question is what is the difference between using char8_t-based strings vs. char-based strings. It really comes down to this: type-based encoding. Any char-based string (char*, char[], string, etc) may be encoded in UTF-8. But … Read more

The reasoning behind Clang’s implementation of std::function’s move semantics

It is a bug in libc++ that cannot be immediately fixed because it would break ABI. Apparently, it is a conforming implementation, although obviously it is often suboptimal. It’s not clear exactly why the Clang devs made such an implementation choice in the first place (although maybe if you’re really lucky, someone from Clang will … Read more