Skip to content

Commit 3df1a64

Browse files
Arthur O'Dwyerldionne
Arthur O'Dwyer
andcommitted
[libc++] Add a test for std::ssize's SFINAE
Inspired by https://reviews.llvm.org/D120684#inline-1157644 and subsequent LWG discussion. See http://wg21.link/LWG3207 for additional context. Differential Revision: https://reviews.llvm.org/D121154 Co-authored-by: Louis Dionne <[email protected]>
1 parent bd095b5 commit 3df1a64

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17
10+
11+
// <iterator>
12+
13+
// template <class T, ptrdiff_t N>
14+
// constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept;
15+
//
16+
// This test checks that the library implements LWG3207 as not-a-defect.
17+
// `clang -m32` is an example of a configuration where using ptrdiff_t
18+
// instead of size_t in std::ssize has an observable SFINAE effect.
19+
//
20+
// REQUIRES: 32-bit-pointer
21+
22+
#include <iterator>
23+
#include <climits>
24+
#include <cstddef>
25+
26+
// Test the test:
27+
static_assert(sizeof(std::ptrdiff_t) == 4, "Run only on these platforms");
28+
static_assert(sizeof(std::size_t) == 4, "Run only on these platforms");
29+
static_assert(std::size_t(PTRDIFF_MAX) + 1 > std::size_t(PTRDIFF_MAX), "This should always be true");
30+
extern char forming_this_type_must_be_valid_on_this_platform[std::size_t(PTRDIFF_MAX) + 1];
31+
32+
// The actual test:
33+
template <class T>
34+
concept HasSsize = requires(T&& t) { std::ssize(t); };
35+
36+
static_assert(HasSsize<char[std::size_t(PTRDIFF_MAX)]>);
37+
static_assert(!HasSsize<char[std::size_t(PTRDIFF_MAX) + 1]>);

0 commit comments

Comments
 (0)