diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -60,6 +60,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 @@ -42,6 +42,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 = {}); @@ -1772,6 +1775,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.in b/libcxx/include/module.modulemap.in --- a/libcxx/include/module.modulemap.in +++ b/libcxx/include/module.modulemap.in @@ -305,6 +305,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/no_unique_address.compile.pass.cpp b/libcxx/test/std/algorithms/algorithms.results/no_unique_address.compile.pass.cpp --- a/libcxx/test/std/algorithms/algorithms.results/no_unique_address.compile.pass.cpp +++ b/libcxx/test/std/algorithms/algorithms.results/no_unique_address.compile.pass.cpp @@ -54,6 +54,10 @@ static_assert(sizeof(std::ranges::in_out_out_result) == sizeof(int)); static_assert(sizeof(std::ranges::in_out_out_result) == 3); +static_assert(sizeof(std::ranges::out_value_result) == sizeof(int)); +static_assert(sizeof(std::ranges::out_value_result) == sizeof(int)); +static_assert(sizeof(std::ranges::out_value_result) == 2); + // In min_max_result both elements have the same type, so they can't have the same address. // So the only way to test that [[no_unique_address]] is used is to have it in another struct struct MinMaxNoUniqueAddress { 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,97 @@ +//===----------------------------------------------------------------------===// +// +// 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" + +using std::ranges::out_value_result; + +struct A { + explicit A(int); +}; +// no implicit conversion +static_assert(!std::is_constructible_v, out_value_result>); + +struct B { + B(int); +}; +// implicit conversion +static_assert(std::is_constructible_v, out_value_result>); +static_assert(std::is_constructible_v, out_value_result&>); +static_assert(std::is_constructible_v, const out_value_result>); +static_assert(std::is_constructible_v, const out_value_result&>); + +struct C { + C(int&); +}; +static_assert(!std::is_constructible_v, out_value_result&>); + +// has to be convertible via const& +static_assert(std::is_convertible_v&, out_value_result>); +static_assert(std::is_convertible_v&, out_value_result>); +static_assert(std::is_convertible_v&&, out_value_result>); +static_assert(std::is_convertible_v&&, 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, out_value_result>); +static_assert(!std::is_convertible_v, out_value_result>); + +template +struct ConstructibleFrom { + constexpr ConstructibleFrom(T c) : content{c} {} + T content; +}; + +constexpr bool test() { + { // check that the conversion operator works + out_value_result res{10, 0.}; + assert(res.out == 10); + assert(res.value == 0.); + out_value_result, ConstructibleFrom> res2 = res; + assert(res2.out.content == 10); + assert(res2.value.content == 0.); + } + { // check that out_value_result isn't overconstrained w.r.t. move/copy constuctors + 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); + } + { // check that structured bindings work + auto [out, value] = out_value_result{1, 2}; + assert(out == 1); + assert(value == 2); + } + + return true; +} + +int main(int, char**) { + test(); + static_assert(test()); + + return 0; +}