up-cpp icon indicating copy to clipboard operation
up-cpp copied to clipboard

Consider switching to C++20

Open gregmedd opened this issue 1 year ago • 5 comments

We have received feedback that many consumers of this library have already migrated to C++20. We should evaluate if it is safe to roll forward so that newer features can be used in the interface and implementation.

gregmedd avatar May 20 '24 16:05 gregmedd

The library currently targets C++17. If there are no particular restrictions moving up to C++ 20, we should do it sooner rather than later.

gregmedd avatar May 21 '24 20:05 gregmedd

I've run into a bit of a snag: gcc 11 and clang 13 are not fully C++20 compliant. We would likely need to bump our minimum compiler versions up to gcc 12 and clang 15 to switch to C++20.

Compiler Explorer comparing C++20 support in the various compilers.

gregmedd avatar May 24 '24 18:05 gregmedd

What advantages of C++20 do we need so much to introduce such strict restrictions? I think that it would be nice for the library to maintain compatibility with previous versions of compilers as much as possible without cutting down on functionality.

sashacmc avatar May 27 '24 09:05 sashacmc

There are several advantages, mostly in improved language features. An example is std::atomic<std::weak_ptr<T>> which replaces the need for a custom implementation involving a mutex, condition variable, and spurious wake handler to achieve the same capabilities.

C++20 adds more constexpr support to the standard library, mathematical constants, spans, std::jthread, std::source_location, the format library, the spaceship comparison operator, bit_cast, many spec fixes and improvements, and so much more.

The problem is that staying behind for compatibility locks us out of tools and features that help us write better, safer C++ code. We aren't talking about early adoption here; C++20 is a 4-year-old standard, GCC 12 was released 2 years ago, and Clang 15 is almost 2 years old. Incidentally, that's the same age GCC 11 and Clang 13 (the current minimum requirements) were when this repo was created a year ago.

If projects were able to use a 2-year-old compiler last year, why is that not possible now?

What is the disadvantage of enabling C++20?

gregmedd avatar May 30 '24 00:05 gregmedd

What is the disadvantage of enabling C++20?

I don't know the target client for this project, but the automotive industry as a whole is pretty legacy dependent. For example, in 2020, working in one company that supplied a library for in-vehicle infotainment, we switched from C++98 to C++11 with great resistance from clients.

And for example, at the moment, GCC 11 is the default compiler in Ubuntu 22.04 LTS, which will be the main build platform for at least a couple of years.

sashacmc avatar May 30 '24 07:05 sashacmc