Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++][ranges] Add ranges::min_max_result
Reviewed By: Quuxplusone, #libc Spies: libcxx-commits, mgorny Differential Revision: https://reviews.llvm.org/D119751
- Loading branch information
1 parent
6766ece
commit 807766b
Showing
6 changed files
with
164 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// -*- C++ -*- | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___ALGORITHM_MIN_MAX_RESULT_H | ||
#define _LIBCPP___ALGORITHM_MIN_MAX_RESULT_H | ||
|
||
#include <__concepts/convertible_to.h> | ||
#include <__config> | ||
#include <__utility/move.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_PUSH_MACROS | ||
#include <__undef_macros> | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
#if!defined(_LIBCPP_HAS_NO_CONCEPTS) && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) | ||
|
||
namespace ranges { | ||
|
||
template <class _T1> | ||
struct min_max_result { | ||
[[no_unique_address]] _T1 min; | ||
[[no_unique_address]] _T1 max; | ||
|
||
template <class _T2> | ||
requires convertible_to<const _T1&, _T2> | ||
_LIBCPP_HIDE_FROM_ABI constexpr operator min_max_result<_T2>() const & { | ||
return {min, max}; | ||
} | ||
|
||
template <class _T2> | ||
requires convertible_to<_T1, _T2> | ||
_LIBCPP_HIDE_FROM_ABI constexpr operator min_max_result<_T2>() && { | ||
return {std::move(min), std::move(max)}; | ||
} | ||
}; | ||
|
||
} // namespace ranges | ||
|
||
#endif | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
_LIBCPP_POP_MACROS | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
libcxx/test/libcxx/diagnostics/detail.headers/algorithm/min_max_result.module.verify.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// REQUIRES: modules-build | ||
|
||
// WARNING: This test was generated by 'generate_private_header_tests.py' | ||
// and should not be edited manually. | ||
|
||
// expected-error@*:* {{use of private header from outside its module: '__algorithm/min_max_result.h'}} | ||
#include <__algorithm/min_max_result.h> |
84 changes: 84 additions & 0 deletions
84
libcxx/test/std/algorithms/algorithms.results/min_max_result.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||
// UNSUPPORTED: libcpp-no-concepts | ||
// UNSUPPORTED: libcpp-has-no-incomplete-ranges | ||
|
||
// template <class I1, class I2> | ||
// struct min_max_result; | ||
|
||
#include <algorithm> | ||
#include <cassert> | ||
#include <type_traits> | ||
|
||
#include "MoveOnly.h" | ||
|
||
template <class T> | ||
struct ConvertibleFrom { | ||
constexpr ConvertibleFrom(T c) : content{c} {} | ||
T content; | ||
}; | ||
|
||
struct A { | ||
explicit A(int); | ||
}; | ||
static_assert(!std::is_constructible_v<std::ranges::min_max_result<A>, std::ranges::min_max_result<int>>); | ||
|
||
struct B { | ||
B(const int&); | ||
B(int&&); | ||
}; | ||
static_assert(std::is_constructible_v<std::ranges::min_max_result<B>, std::ranges::min_max_result<int>>); | ||
static_assert(std::is_constructible_v<std::ranges::min_max_result<B>, std::ranges::min_max_result<int>&>); | ||
static_assert(std::is_constructible_v<std::ranges::min_max_result<B>, const std::ranges::min_max_result<int>>); | ||
static_assert(std::is_constructible_v<std::ranges::min_max_result<B>, const std::ranges::min_max_result<int>&>); | ||
|
||
struct C { | ||
C(int&); | ||
}; | ||
static_assert(!std::is_constructible_v<std::ranges::min_max_result<C>, std::ranges::min_max_result<int>&>); | ||
|
||
static_assert(std::is_convertible_v<std::ranges::min_max_result<int>&, std::ranges::min_max_result<long>>); | ||
static_assert(std::is_convertible_v<const std::ranges::min_max_result<int>&, std::ranges::min_max_result<long>>); | ||
static_assert(std::is_convertible_v<std::ranges::min_max_result<int>&&, std::ranges::min_max_result<long>>); | ||
static_assert(std::is_convertible_v<const std::ranges::min_max_result<int>&&, std::ranges::min_max_result<long>>); | ||
|
||
struct NotConvertible {}; | ||
static_assert(!std::is_convertible_v<std::ranges::min_max_result<NotConvertible>, std::ranges::min_max_result<int>>); | ||
|
||
constexpr bool test() { | ||
{ | ||
std::ranges::min_max_result<double> res{10, 1}; | ||
assert(res.min == 10); | ||
assert(res.max == 1); | ||
std::ranges::min_max_result<ConvertibleFrom<int>> res2 = res; | ||
assert(res2.min.content == 10); | ||
assert(res2.max.content == 1); | ||
} | ||
{ | ||
std::ranges::min_max_result<MoveOnly> res{MoveOnly{}, MoveOnly{}}; | ||
assert(res.min.get() == 1); | ||
assert(res.max.get() == 1); | ||
[[maybe_unused]] auto res2 = static_cast<std::ranges::min_max_result<MoveOnly>>(std::move(res)); | ||
assert(res.min.get() == 0); | ||
assert(res.max.get() == 0); | ||
} | ||
auto [min, max] = std::ranges::min_max_result<int>{1, 2}; | ||
assert(min == 1); | ||
assert(max == 2); | ||
|
||
return true; | ||
} | ||
|
||
int main(int, char**) { | ||
test(); | ||
static_assert(test()); | ||
|
||
return 0; | ||
} |