C++20 has landed, bringing with it Concepts. If a project were to start now and target only C++20 and later standards, would it be appropriate to say previous forms of constraints are now superseded by Concepts and the requires
clause?
Are there any cases where enable_if
or void_t
are still required, where Concepts cannot be made to replace them?
For example, std::bit_cast
is constrained to require both To
and From
types to be of the same size and both types to be trivially copyable.
From cppreference:
This overload participates in overload resolution only if sizeof(To) == sizeof(From) and both To and From are TriviallyCopyable types.
The MSVC standard library constrains this through an enable_if_t
expression, while libcxx opts for a requires
clause.
MSVC:
enable_if_t<conjunction_v<bool_constant<sizeof(_To) == sizeof(_From)>, is_trivially_copyable<_To>, is_trivially_copyable<_From>>, int> = 0
libcxx:
requires(sizeof(_ToType) == sizeof(_FromType) &&
is_trivially_copyable_v<_ToType> &&
is_trivially_copyable_v<_FromType>)
They perform the same logical operations through different language features.
To reiterate my question, are there any cases where this translation from one constraint method to another is not possible? I understand there are a lot of things Concepts and requires
can do that enable_if
and/or void_t
can't, but can the same be said looking in the other direction? Would a wholly new codebase targeting C++20 and later ever need to fall back on these older language constructions?
requires
because they needed to support compilers that didn't implement C++20 concepts fully at the time. See github.com/microsoft/STL/pull/1749. I don't think there is any situation in which SFINAE withenable_if
/void_t
cannot be replaced by arequires
clause on a fully conforming compiler. – Thursdayconcept
constrained templates should generate cleaner error diagnostics in future. This was 1 of the intentions behind the idea, but currently support for this feature is poor. With oldenable_if
hacks, there's no luck for good diagnostics. In new code, old methods just bring more irreadability. A demand for good diagnostics withconcept
s must become the focus of C++ community. – Koopman