Skip to content

Commit

Permalink
Implement signature changes for char_traits in WG21-N2349 (#3739)
Browse files Browse the repository at this point in the history
Co-authored-by: Stephan T. Lavavej <[email protected]>
  • Loading branch information
frederick-vs-ja and StephanTLavavej authored Jun 15, 2023
1 parent 3e00fa3 commit 15aea98
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 18 deletions.
36 changes: 18 additions & 18 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -190,27 +190,27 @@ struct _Char_traits { // properties of a string or stream element
}
}

_NODISCARD static constexpr bool eq(const _Elem& _Left, const _Elem& _Right) noexcept {
_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 {
_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 {
_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 {
_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 {
_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 {
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
return _Meta != eof() ? _Meta : !eof();
}

Expand Down Expand Up @@ -302,27 +302,27 @@ public:
_Left = _Right;
}

_NODISCARD static constexpr bool eq(const _Elem& _Left, const _Elem& _Right) noexcept {
_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 {
_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 {
_NODISCARD static constexpr _Elem to_char_type(const int_type _Meta) noexcept {
return _Meta;
}

_NODISCARD static constexpr int_type to_int_type(const _Elem& _Ch) noexcept {
_NODISCARD static constexpr int_type to_int_type(const _Elem _Ch) noexcept {
return _Ch;
}

_NODISCARD static constexpr bool eq_int_type(const int_type& _Left, const int_type& _Right) noexcept {
_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 {
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
return _Meta != eof() ? _Meta : static_cast<int_type>(!eof());
}

Expand Down Expand Up @@ -446,27 +446,27 @@ public:
_Left = _Right;
}

_NODISCARD static constexpr bool eq(const _Elem& _Left, const _Elem& _Right) noexcept {
_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 {
_NODISCARD static constexpr bool lt(const _Elem _Left, const _Elem _Right) noexcept {
return static_cast<unsigned char>(_Left) < static_cast<unsigned char>(_Right);
}

_NODISCARD static constexpr _Elem to_char_type(const int_type& _Meta) noexcept {
_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 {
_NODISCARD static constexpr int_type to_int_type(const _Elem _Ch) noexcept {
return static_cast<unsigned char>(_Ch);
}

_NODISCARD static constexpr bool eq_int_type(const int_type& _Left, const int_type& _Right) noexcept {
_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 {
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
return _Meta != eof() ? _Meta : !eof();
}

Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ tests\GH_003246_cmath_narrowing
tests\GH_003570_allocate_at_least
tests\GH_003617_vectorized_meow_element
tests\GH_003676_format_large_hh_mm_ss_values
tests\GH_003735_char_traits_signatures
tests\LWG2381_num_get_floating_point
tests\LWG2597_complex_branch_cut
tests\LWG3018_shared_ptr_function
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/GH_003735_char_traits_signatures/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\usual_matrix.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <cstdint>
#include <cstdio>
#include <cwchar>
#include <string>
#include <type_traits>

#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)

using namespace std;

// Also ensure that the member type int_type is correct for standard specializations.
STATIC_ASSERT(is_same_v<char_traits<char>::int_type, int>);
#ifdef __cpp_char8_t
STATIC_ASSERT(is_same_v<char_traits<char8_t>::int_type, unsigned int>);
#endif // __cpp_char8_t
STATIC_ASSERT(is_same_v<char_traits<char16_t>::int_type, uint_least16_t>);
STATIC_ASSERT(is_same_v<char_traits<char32_t>::int_type, uint_least32_t>);
STATIC_ASSERT(is_same_v<char_traits<wchar_t>::int_type, wint_t>);

template <class CharT>
void test_n2349_char_traits_signatures() {
using IntT = typename char_traits<CharT>::int_type;

volatile CharT c{};
volatile IntT i{};

(void) char_traits<CharT>::eq(c, c);
(void) char_traits<CharT>::lt(c, c);
(void) char_traits<CharT>::not_eof(i);
(void) char_traits<CharT>::to_char_type(i);
(void) char_traits<CharT>::to_int_type(c);
(void) char_traits<CharT>::eq_int_type(i, i);
}

void test_n2349_char_traits_signatures_all() {
test_n2349_char_traits_signatures<char>();
#ifdef __cpp_char8_t
test_n2349_char_traits_signatures<char8_t>();
#endif // __cpp_char8_t
test_n2349_char_traits_signatures<char16_t>();
test_n2349_char_traits_signatures<char32_t>();
test_n2349_char_traits_signatures<wchar_t>();
}

0 comments on commit 15aea98

Please sign in to comment.