diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -58,6 +58,7 @@ __algorithm/next_permutation.h __algorithm/none_of.h __algorithm/nth_element.h + __algorithm/out_value_result.h __algorithm/partial_sort.h __algorithm/partial_sort_copy.h __algorithm/partition.h diff --git a/libcxx/include/__algorithm/out_value_result.h b/libcxx/include/__algorithm/out_value_result.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/out_value_result.h @@ -0,0 +1,53 @@ +// -*- 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_OUT_VALUE_RESULT_H +#define _LIBCPP___ALGORITHM_OUT_VALUE_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_BEGIN_NAMESPACE_STD + +#if !defined(_LIBCPP_HAS_NO_CONCEPTS) + +namespace ranges { + +template +struct out_value_result { + _LIBCPP_NO_UNIQUE_ADDRESS _O1 out; + _LIBCPP_NO_UNIQUE_ADDRESS _T1 value; + + template + requires convertible_to && convertible_to + _LIBCPP_HIDE_FROM_ABI constexpr + operator out_value_result<_O2, _T2>() const & { + return {out, value}; + } + + template + requires convertible_to<_O1, _O2> && convertible_to<_T1, _T2> + _LIBCPP_HIDE_FROM_ABI constexpr + operator out_value_result<_O2, _T2>() && { + return {std::move(out), std::move(value)}; + } +}; + +} // namespace ranges + +#endif // !defined(_LIBCPP_HAS_NO_CONCEPTS) + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___ALGORITHM_OUT_VALUE_RESULT_H diff --git a/libcxx/include/algorithm b/libcxx/include/algorithm --- a/libcxx/include/algorithm +++ b/libcxx/include/algorithm @@ -37,6 +37,9 @@ template struct in_found_result; // since C++20 + template + struct out_value_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 = {}); @@ -767,6 +770,7 @@ #include <__algorithm/next_permutation.h> #include <__algorithm/none_of.h> #include <__algorithm/nth_element.h> +#include <__algorithm/out_value_result.h> #include <__algorithm/partial_sort.h> #include <__algorithm/partial_sort_copy.h> #include <__algorithm/partition.h> diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -286,6 +286,7 @@ module next_permutation { private header "__algorithm/next_permutation.h" } module none_of { private header "__algorithm/none_of.h" } module nth_element { private header "__algorithm/nth_element.h" } + module out_value_result { private header "__algorithm/out_value_result.h" } module partial_sort { private header "__algorithm/partial_sort.h" } module partial_sort_copy { private header "__algorithm/partial_sort_copy.h" } module partition { private header "__algorithm/partition.h" } diff --git a/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/out_value_result.module.verify.cpp b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/out_value_result.module.verify.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/diagnostics/detail.headers/algorithm/out_value_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/out_value_result.h'}} +#include <__algorithm/out_value_result.h> diff --git a/libcxx/test/std/algorithms/algorithms.results/out_value_result.pass.cpp b/libcxx/test/std/algorithms/algorithms.results/out_value_result.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/algorithms.results/out_value_result.pass.cpp @@ -0,0 +1,92 @@ +//===----------------------------------------------------------------------===// +// +// 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 out_value_result; + +#include +#include +#include + +#include "MoveOnly.h" + +struct A { + explicit A(int); +}; +// no implicit conversion +static_assert(!std::is_constructible_v, std::ranges::out_value_result>); + +struct B { + B(int); +}; +// implicit conversion +static_assert(std::is_constructible_v, std::ranges::out_value_result>); +static_assert(std::is_constructible_v, std::ranges::out_value_result&>); +static_assert(std::is_constructible_v, const std::ranges::out_value_result>); +static_assert(std::is_constructible_v, const std::ranges::out_value_result&>); + +struct C { + C(int&); +}; +static_assert(!std::is_constructible_v, std::ranges::out_value_result&>); + +// has to be convertible via const& +static_assert(std::is_convertible_v&, std::ranges::out_value_result>); +static_assert(std::is_convertible_v&, std::ranges::out_value_result>); +static_assert(std::is_convertible_v&&, std::ranges::out_value_result>); +static_assert(std::is_convertible_v&&, std::ranges::out_value_result>); + +// should be move constructible +static_assert(std::is_move_constructible_v>); +static_assert(std::is_move_constructible_v>); + +struct NotConvertible {}; +// conversions should not work if there is no conversion +static_assert(!std::is_convertible_v, std::ranges::out_value_result>); +static_assert(!std::is_convertible_v, std::ranges::out_value_result>); + +template +struct ConstructibleFrom { + constexpr ConstructibleFrom(T c) : content{c} {} + T content; +}; + +constexpr bool test() { + { + std::ranges::out_value_result res{10L, 0.}; + assert(res.out == 10); + assert(res.value == 0.); + std::ranges::out_value_result, ConstructibleFrom> res2 = res; + assert(res2.out.content == 10); + assert(res2.value.content == 0.); + } + { + std::ranges::out_value_result res{MoveOnly{}, 0}; + assert(res.out.get() == 1); + [[maybe_unused]] auto res2 = static_cast>(std::move(res)); + assert(res.out.get() == 0); + } + { + auto [out, value] = std::ranges::out_value_result{1, 2}; + assert(out == 1); + assert(value == 2); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +}