Skip to content

<string>: Consider improving conformance for char_traits #3735

@achabense

Description

@achabense
  1. According to the standard(ref), char_traits::eq/lt/to_char_type/to_int_type/eq_int_type/not_eof should take values instead of references as parameters.
    (
    Also in
    https://github.com/microsoft/STL/blob/a62109595b6d89e08172fdf4beb75a2670fe0cc9/stl/inc/xstring#L305-L327?plain=1
    https://github.com/microsoft/STL/blob/a62109595b6d89e08172fdf4beb75a2670fe0cc9/stl/inc/xstring#L449-L471?plain=1
    )

    STL/stl/inc/xstring

    Lines 193 to 215 in a621095

    _NODISCARD static constexpr bool eq(const _Elem& _Left, const _Elem& _Right) noexcept {
    return _Left == _Right;
    }
    _NODISCARD static constexpr bool lt(const _Elem& _Left, const _Elem& _Right) noexcept {
    return _Left < _Right;
    }
    _NODISCARD static constexpr _Elem to_char_type(const int_type& _Meta) noexcept {
    return static_cast<_Elem>(_Meta);
    }
    _NODISCARD static constexpr int_type to_int_type(const _Elem& _Ch) noexcept {
    return static_cast<int_type>(_Ch);
    }
    _NODISCARD static constexpr bool eq_int_type(const int_type& _Left, const int_type& _Right) noexcept {
    return _Left == _Right;
    }
    _NODISCARD static constexpr int_type not_eof(const int_type& _Meta) noexcept {
    return _Meta != eof() ? _Meta : !eof();
    }

  2. Also, int_types for char_traits<char16_t>, <char32_t> and <wchar_t> should be uint_least16_t, uint_least32_t and wint_t respectively. Currently they are defined as unsigned short, unsigned int and unsigned short. --They just works now. However, I think it will be better to use aliases directly, or at least add some static_asserts(e.g. static_assert(std::is_same_v<wint_t,unsigned short>).

    STL/stl/inc/xstring

    Lines 337 to 344 in a621095

    template <>
    struct char_traits<char16_t> : _WChar_traits<char16_t> {};
    template <>
    struct char_traits<char32_t> : _Char_traits<char32_t, unsigned int> {};
    template <>
    struct char_traits<wchar_t> : _WChar_traits<wchar_t> {};

    (char_traits<char>,<char8_t> has no such issue)

  3. (off topic) We can safely add noexcept /*strengthened*/ for this function:

    STL/stl/inc/xstring

    Lines 1402 to 1404 in a621095

    _NODISCARD constexpr int compare(_In_z_ const _Elem* const _Ptr) const { // compare [0, _Mysize) with [_Ptr, <null>)
    return compare(basic_string_view(_Ptr));
    }

  4. (off topic; question) What does the template(line2547~2549) do here? _Alloc2 is not used in the body, and _Alloc is directly used as allocator in other member functions.
    (
    Also here; the only another use of this pattern
    https://github.com/microsoft/STL/blob/a62109595b6d89e08172fdf4beb75a2670fe0cc9/stl/inc/xstring#L2560-L2566?plain=1
    )

    STL/stl/inc/xstring

    Lines 2547 to 2553 in a621095

    #if _HAS_CXX17
    template <class _Alloc2 = _Alloc, enable_if_t<_Is_allocator<_Alloc2>::value, int> = 0>
    #endif // _HAS_CXX17
    _CONSTEXPR20 basic_string(_In_z_ const _Elem* const _Ptr, const _Alloc& _Al)
    : _Mypair(_One_then_variadic_args_t{}, _Al) {
    _Construct<_Construct_strategy::_From_ptr>(_Ptr, _Convert_size<size_type>(_Traits::length(_Ptr)));
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions