diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -48,6 +48,7 @@ __algorithm/merge.h __algorithm/min.h __algorithm/min_element.h + __algorithm/min_max_result.h __algorithm/minmax.h __algorithm/minmax_element.h __algorithm/mismatch.h diff --git a/libcxx/include/__algorithm/min_max_result.h b/libcxx/include/__algorithm/min_max_result.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/min_max_result.h @@ -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 +struct min_max_result { + [[no_unique_address]] _T1 min; + [[no_unique_address]] _T1 max; + + template + requires convertible_to + _LIBCPP_HIDE_FROM_ABI constexpr operator min_max_result<_T2>() const & { + return {min, max}; + } + + template + 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 diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -20,17 +20,20 @@ namespace ranges { template - struct in_fun_result; // since C++20 + struct in_fun_result; // since C++20 template - struct in_in_result; // since C++20 + struct in_in_result; // since C++20 template - struct in_in_out_result; // since C++20 + struct in_in_out_result; // since C++20 template struct in_out_out_result; // since C++20 + template + struct min_max_result; // since C++20 + template S, class Proj = identity, indirect_strict_weak_order> Comp = ranges::less> // since C++20 constexpr I min_element(I first, S last, Comp comp = {}, Proj proj = {}); @@ -738,6 +741,7 @@ #include <__algorithm/merge.h> #include <__algorithm/min.h> #include <__algorithm/min_element.h> +#include <__algorithm/min_max_result.h> #include <__algorithm/minmax.h> #include <__algorithm/minmax_element.h> #include <__algorithm/mismatch.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -270,6 +270,7 @@ module merge { private header "__algorithm/merge.h" } module min { private header "__algorithm/min.h" } module min_element { private header "__algorithm/min_element.h" } + module min_max_result { private header "__algorithm/min_max_result.h" } module minmax { private header "__algorithm/minmax.h" } module minmax_element { private header "__algorithm/minmax_element.h" } module mismatch { private header "__algorithm/mismatch.h" } diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/min_max_result.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/min_max_result.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/min_max_result.module.verify.cpp @@ -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> diff --git a/libcxx/test/std/algorithms/algorithms.results/min_max_result.pass.cpp b/libcxx/test/std/algorithms/algorithms.results/min_max_result.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/algorithms.results/min_max_result.pass.cpp @@ -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 +// struct min_max_result; + +#include +#include +#include + +#include "MoveOnly.h" + +template +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>); + +struct B { + B(const int&); + B(int&&); +}; +static_assert(std::is_constructible_v, std::ranges::min_max_result>); +static_assert(std::is_constructible_v, std::ranges::min_max_result&>); +static_assert(std::is_constructible_v, const std::ranges::min_max_result>); +static_assert(std::is_constructible_v, const std::ranges::min_max_result&>); + +struct C { + C(int&); +}; +static_assert(!std::is_constructible_v, std::ranges::min_max_result&>); + +static_assert(std::is_convertible_v&, std::ranges::min_max_result>); +static_assert(std::is_convertible_v&, std::ranges::min_max_result>); +static_assert(std::is_convertible_v&&, std::ranges::min_max_result>); +static_assert(std::is_convertible_v&&, std::ranges::min_max_result>); + +struct NotConvertible {}; +static_assert(!std::is_convertible_v, std::ranges::min_max_result>); + +constexpr bool test() { + { + std::ranges::min_max_result res{10, 1}; + assert(res.min == 10); + assert(res.max == 1); + std::ranges::min_max_result> res2 = res; + assert(res2.min.content == 10); + assert(res2.max.content == 1); + } + { + std::ranges::min_max_result res{MoveOnly{}, MoveOnly{}}; + assert(res.min.get() == 1); + assert(res.max.get() == 1); + [[maybe_unused]] auto res2 = static_cast>(std::move(res)); + assert(res.min.get() == 0); + assert(res.max.get() == 0); + } + auto [min, max] = std::ranges::min_max_result{1, 2}; + assert(min == 1); + assert(max == 2); + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +}