From the many changes of C++20, let’s focus today on explicit(bool). This feature is described in P0892R2. The explicit keyword avoids implicit conversions by unexpectedly calling a constructor. With C++20, it is now possible to have a boolean condition as a “parameter” to the keyword.
How it works
This simple code makes it easy to understand how explicit(bool) works:
constexpr bool ENABLE_EXPLICIT = false;
struct Foo {
explicit(ENABLE_EXPLICIT)
Foo(int) {}
};
Foo a = 1;
This code compiles fine, but change ENABLE_EXPLICIT to true and you will get an error with clang-10.0.0: