Skip to content

Commit 15aea98

Browse files
Implement signature changes for char_traits in WG21-N2349 (#3739)
Co-authored-by: Stephan T. Lavavej <[email protected]>
1 parent 3e00fa3 commit 15aea98

File tree

4 files changed

+69
-18
lines changed

4 files changed

+69
-18
lines changed

stl/inc/xstring

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -190,27 +190,27 @@ struct _Char_traits { // properties of a string or stream element
190190
}
191191
}
192192

193-
_NODISCARD static constexpr bool eq(const _Elem& _Left, const _Elem& _Right) noexcept {
193+
_NODISCARD static constexpr bool eq(const _Elem _Left, const _Elem _Right) noexcept {
194194
return _Left == _Right;
195195
}
196196

197-
_NODISCARD static constexpr bool lt(const _Elem& _Left, const _Elem& _Right) noexcept {
197+
_NODISCARD static constexpr bool lt(const _Elem _Left, const _Elem _Right) noexcept {
198198
return _Left < _Right;
199199
}
200200

201-
_NODISCARD static constexpr _Elem to_char_type(const int_type& _Meta) noexcept {
201+
_NODISCARD static constexpr _Elem to_char_type(const int_type _Meta) noexcept {
202202
return static_cast<_Elem>(_Meta);
203203
}
204204

205-
_NODISCARD static constexpr int_type to_int_type(const _Elem& _Ch) noexcept {
205+
_NODISCARD static constexpr int_type to_int_type(const _Elem _Ch) noexcept {
206206
return static_cast<int_type>(_Ch);
207207
}
208208

209-
_NODISCARD static constexpr bool eq_int_type(const int_type& _Left, const int_type& _Right) noexcept {
209+
_NODISCARD static constexpr bool eq_int_type(const int_type _Left, const int_type _Right) noexcept {
210210
return _Left == _Right;
211211
}
212212

213-
_NODISCARD static constexpr int_type not_eof(const int_type& _Meta) noexcept {
213+
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
214214
return _Meta != eof() ? _Meta : !eof();
215215
}
216216

@@ -302,27 +302,27 @@ public:
302302
_Left = _Right;
303303
}
304304

305-
_NODISCARD static constexpr bool eq(const _Elem& _Left, const _Elem& _Right) noexcept {
305+
_NODISCARD static constexpr bool eq(const _Elem _Left, const _Elem _Right) noexcept {
306306
return _Left == _Right;
307307
}
308308

309-
_NODISCARD static constexpr bool lt(const _Elem& _Left, const _Elem& _Right) noexcept {
309+
_NODISCARD static constexpr bool lt(const _Elem _Left, const _Elem _Right) noexcept {
310310
return _Left < _Right;
311311
}
312312

313-
_NODISCARD static constexpr _Elem to_char_type(const int_type& _Meta) noexcept {
313+
_NODISCARD static constexpr _Elem to_char_type(const int_type _Meta) noexcept {
314314
return _Meta;
315315
}
316316

317-
_NODISCARD static constexpr int_type to_int_type(const _Elem& _Ch) noexcept {
317+
_NODISCARD static constexpr int_type to_int_type(const _Elem _Ch) noexcept {
318318
return _Ch;
319319
}
320320

321-
_NODISCARD static constexpr bool eq_int_type(const int_type& _Left, const int_type& _Right) noexcept {
321+
_NODISCARD static constexpr bool eq_int_type(const int_type _Left, const int_type _Right) noexcept {
322322
return _Left == _Right;
323323
}
324324

325-
_NODISCARD static constexpr int_type not_eof(const int_type& _Meta) noexcept {
325+
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
326326
return _Meta != eof() ? _Meta : static_cast<int_type>(!eof());
327327
}
328328

@@ -446,27 +446,27 @@ public:
446446
_Left = _Right;
447447
}
448448

449-
_NODISCARD static constexpr bool eq(const _Elem& _Left, const _Elem& _Right) noexcept {
449+
_NODISCARD static constexpr bool eq(const _Elem _Left, const _Elem _Right) noexcept {
450450
return _Left == _Right;
451451
}
452452

453-
_NODISCARD static constexpr bool lt(const _Elem& _Left, const _Elem& _Right) noexcept {
453+
_NODISCARD static constexpr bool lt(const _Elem _Left, const _Elem _Right) noexcept {
454454
return static_cast<unsigned char>(_Left) < static_cast<unsigned char>(_Right);
455455
}
456456

457-
_NODISCARD static constexpr _Elem to_char_type(const int_type& _Meta) noexcept {
457+
_NODISCARD static constexpr _Elem to_char_type(const int_type _Meta) noexcept {
458458
return static_cast<_Elem>(_Meta);
459459
}
460460

461-
_NODISCARD static constexpr int_type to_int_type(const _Elem& _Ch) noexcept {
461+
_NODISCARD static constexpr int_type to_int_type(const _Elem _Ch) noexcept {
462462
return static_cast<unsigned char>(_Ch);
463463
}
464464

465-
_NODISCARD static constexpr bool eq_int_type(const int_type& _Left, const int_type& _Right) noexcept {
465+
_NODISCARD static constexpr bool eq_int_type(const int_type _Left, const int_type _Right) noexcept {
466466
return _Left == _Right;
467467
}
468468

469-
_NODISCARD static constexpr int_type not_eof(const int_type& _Meta) noexcept {
469+
_NODISCARD static constexpr int_type not_eof(const int_type _Meta) noexcept {
470470
return _Meta != eof() ? _Meta : !eof();
471471
}
472472

tests/std/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ tests\GH_003246_cmath_narrowing
225225
tests\GH_003570_allocate_at_least
226226
tests\GH_003617_vectorized_meow_element
227227
tests\GH_003676_format_large_hh_mm_ss_values
228+
tests\GH_003735_char_traits_signatures
228229
tests\LWG2381_num_get_floating_point
229230
tests\LWG2597_complex_branch_cut
230231
tests\LWG3018_shared_ptr_function
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
RUNALL_INCLUDE ..\usual_matrix.lst
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <cstdint>
5+
#include <cstdio>
6+
#include <cwchar>
7+
#include <string>
8+
#include <type_traits>
9+
10+
#define STATIC_ASSERT(...) static_assert(__VA_ARGS__, #__VA_ARGS__)
11+
12+
using namespace std;
13+
14+
// Also ensure that the member type int_type is correct for standard specializations.
15+
STATIC_ASSERT(is_same_v<char_traits<char>::int_type, int>);
16+
#ifdef __cpp_char8_t
17+
STATIC_ASSERT(is_same_v<char_traits<char8_t>::int_type, unsigned int>);
18+
#endif // __cpp_char8_t
19+
STATIC_ASSERT(is_same_v<char_traits<char16_t>::int_type, uint_least16_t>);
20+
STATIC_ASSERT(is_same_v<char_traits<char32_t>::int_type, uint_least32_t>);
21+
STATIC_ASSERT(is_same_v<char_traits<wchar_t>::int_type, wint_t>);
22+
23+
template <class CharT>
24+
void test_n2349_char_traits_signatures() {
25+
using IntT = typename char_traits<CharT>::int_type;
26+
27+
volatile CharT c{};
28+
volatile IntT i{};
29+
30+
(void) char_traits<CharT>::eq(c, c);
31+
(void) char_traits<CharT>::lt(c, c);
32+
(void) char_traits<CharT>::not_eof(i);
33+
(void) char_traits<CharT>::to_char_type(i);
34+
(void) char_traits<CharT>::to_int_type(c);
35+
(void) char_traits<CharT>::eq_int_type(i, i);
36+
}
37+
38+
void test_n2349_char_traits_signatures_all() {
39+
test_n2349_char_traits_signatures<char>();
40+
#ifdef __cpp_char8_t
41+
test_n2349_char_traits_signatures<char8_t>();
42+
#endif // __cpp_char8_t
43+
test_n2349_char_traits_signatures<char16_t>();
44+
test_n2349_char_traits_signatures<char32_t>();
45+
test_n2349_char_traits_signatures<wchar_t>();
46+
}

0 commit comments

Comments
 (0)