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 _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +namespace ranges { + +template +struct out_value_result { + _LIBCPP_NO_UNIQUE_ADDRESS _OutIter1 out; + _LIBCPP_NO_UNIQUE_ADDRESS _Type1 value; + + template + requires convertible_to && convertible_to + _LIBCPP_HIDE_FROM_ABI constexpr + operator out_value_result<_OutIter2, _Type2>() const & { + return {out, value}; + } + + template + requires convertible_to<_OutIter1, _OutIter2> && convertible_to<_Type1, _Type2> + _LIBCPP_HIDE_FROM_ABI constexpr + operator out_value_result<_OutIter2, _Type2>() && { + return {std::move(out), std::move(value)}; + } +}; + +} // namespace ranges + +#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES) + +_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 = {}); @@ -854,6 +857,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/std/algorithms/algorithms.results/in_out_result.pass.cpp b/libcxx/test/std/algorithms/algorithms.results/in_out_result.pass.cpp --- a/libcxx/test/std/algorithms/algorithms.results/in_out_result.pass.cpp +++ b/libcxx/test/std/algorithms/algorithms.results/in_out_result.pass.cpp @@ -90,4 +90,6 @@ int main(int, char**) { test(); static_assert(test()); + + return 0; } 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,95 @@ +//===----------------------------------------------------------------------===// +// +// 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-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{10, 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{}, 10}; + assert(res.out.get() == 1); + assert(res.value == 10); + auto res2 = std::move(res); + assert(res.out.get() == 0); + assert(res.value == 10); + assert(res2.out.get() == 1); + assert(res2.value == 10); + } + { + 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; +}