Date: Fri, 20 Aug 2021 13:19:47 +0200
On 2021-08-20 at 10:41, DBJ via Std-Proposals wrote:
> I can see (for example) static_assert being used in basic_stringview to
> constrain the instantiation of it. Not the type definition.
>
> Thus I am wondering what was/is wrong with using C++20 require, for
> example?
>
> I was told standard specifically allows for wrong types to be defined;
> like eg tuple<void> or basic_stringview<void ***> ...
>
> Is the following synopsis thus not following the standard?
>
> #include <string>
> #include <type_traits>
>
> namespace {
> using namespace std;
>
> template <class _Elem, class _Traits>
> #if __cplusplus >= 202002L
> // C++20 (and later) code
> requires // requires-clause (ad-hoc constraint)
> is_same_v<_Elem, typename _Traits::char_type> &&
> (!is_array_v<_Elem>)&&is_trivial_v<_Elem>&& is_standard_layout_v<_Elem>
> #endif
> class basic_string_view { // wrapper for any kind of contiguous
> character
> // buffer
> public:
> #if __cplusplus < 202002L
> // apparently standard was/is barring std:: types to be constrained?
> // ditto this is instantiation constraint
> // as present in MS STLtoday
> static_assert(is_same_v<_Elem, typename _Traits::char_type>,
> "Bad char_traits for basic_string_view; "
> "N4659 24.4.2 [string.view.template]/1 \"the type "
> "traits::char_type shall name the same type as charT.\"");
>
> static_assert(!is_array_v<_Elem> && is_trivial_v<_Elem> &&
> is_standard_layout_v<_Elem>,
> "The character type of basic_string_view must be a
> non-array "
> "trivial standard-layout type. See N4861 "
> "[strings.general]/1.");
> #endif
> };
>
> using ok_sv = basic_string_view<char, std::char_traits<char> >;
>
> } // namespace
>
> *https://godbolt.org/z/44cWrqqcs <https://godbolt.org/z/44cWrqqcs>*
>
>
In this particular case, the implementaion was added for C++17. There is
nothing saying that it *must* use constraints for C++20. So probably
nobody cared to change it. If it works, don't fix it?
In other places, the standard specifically uses the words "Constraints:"
and "Mandates:" for things that would be implemented using requires and
static_assert, respectively.
Sometimes it is intended to remove some overloads (using "requires") or
to fail hard with a message (static_assert).
> I can see (for example) static_assert being used in basic_stringview to
> constrain the instantiation of it. Not the type definition.
>
> Thus I am wondering what was/is wrong with using C++20 require, for
> example?
>
> I was told standard specifically allows for wrong types to be defined;
> like eg tuple<void> or basic_stringview<void ***> ...
>
> Is the following synopsis thus not following the standard?
>
> #include <string>
> #include <type_traits>
>
> namespace {
> using namespace std;
>
> template <class _Elem, class _Traits>
> #if __cplusplus >= 202002L
> // C++20 (and later) code
> requires // requires-clause (ad-hoc constraint)
> is_same_v<_Elem, typename _Traits::char_type> &&
> (!is_array_v<_Elem>)&&is_trivial_v<_Elem>&& is_standard_layout_v<_Elem>
> #endif
> class basic_string_view { // wrapper for any kind of contiguous
> character
> // buffer
> public:
> #if __cplusplus < 202002L
> // apparently standard was/is barring std:: types to be constrained?
> // ditto this is instantiation constraint
> // as present in MS STLtoday
> static_assert(is_same_v<_Elem, typename _Traits::char_type>,
> "Bad char_traits for basic_string_view; "
> "N4659 24.4.2 [string.view.template]/1 \"the type "
> "traits::char_type shall name the same type as charT.\"");
>
> static_assert(!is_array_v<_Elem> && is_trivial_v<_Elem> &&
> is_standard_layout_v<_Elem>,
> "The character type of basic_string_view must be a
> non-array "
> "trivial standard-layout type. See N4861 "
> "[strings.general]/1.");
> #endif
> };
>
> using ok_sv = basic_string_view<char, std::char_traits<char> >;
>
> } // namespace
>
> *https://godbolt.org/z/44cWrqqcs <https://godbolt.org/z/44cWrqqcs>*
>
>
In this particular case, the implementaion was added for C++17. There is
nothing saying that it *must* use constraints for C++20. So probably
nobody cared to change it. If it works, don't fix it?
In other places, the standard specifically uses the words "Constraints:"
and "Mandates:" for things that would be implemented using requires and
static_assert, respectively.
Sometimes it is intended to remove some overloads (using "requires") or
to fail hard with a message (static_assert).
Received on 2021-08-20 06:20:08