diff --git a/libcxx/include/CMakeLists.txt b/libcxx/include/CMakeLists.txt --- a/libcxx/include/CMakeLists.txt +++ b/libcxx/include/CMakeLists.txt @@ -67,6 +67,7 @@ __algorithm/replace_copy.h __algorithm/replace_if.h __algorithm/replace.h + __algorithm/results.h __algorithm/reverse_copy.h __algorithm/reverse.h __algorithm/rotate_copy.h diff --git a/libcxx/include/__algorithm/results.h b/libcxx/include/__algorithm/results.h new file mode 100644 --- /dev/null +++ b/libcxx/include/__algorithm/results.h @@ -0,0 +1,45 @@ +// -*- 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_IN_IN_RESULT_H +#define _LIBCPP___ALGORITHM_IN_IN_RESULT_H + +#include <__config> +#include <__concepts/convertible_to.h> +#include <__utility/move.h> + +_LIBCPP_BEGIN_NAMESPACE_STD + +#ifndef _LIBCPP_HAS_NO_RANGES + +namespace ranges { +template +struct in_in_result { + [[no_unique_address]] _I1 in1; + [[no_unique_address]] _I2 in2; + + template + requires convertible_to && convertible_to + constexpr operator in_in_result<_II1, _II2>() const & { + return {in1, in2}; + } + + template + requires convertible_to<_I1, _II1> && convertible_to<_I2, _II2> + constexpr operator in_in_result<_II1, _II2>() && { + return {std::move(in1), std::move(in2)}; + } +}; +} // namespace ranges + +#endif // _LIBCPP_HAS_NO_RANGES + +_LIBCPP_END_NAMESPACE_STD + +#endif // _LIBCPP___ALGORITHM_IN_IN_RESULT_H diff --git a/libcxx/include/module.modulemap b/libcxx/include/module.modulemap --- a/libcxx/include/module.modulemap +++ b/libcxx/include/module.modulemap @@ -287,6 +287,7 @@ module replace_copy { private header "__algorithm/replace_copy.h" } module replace_copy_if { private header "__algorithm/replace_copy_if.h" } module replace_if { private header "__algorithm/replace_if.h" } + module results { private header "__algorithm/results.h" } module reverse { private header "__algorithm/reverse.h" } module reverse_copy { private header "__algorithm/reverse_copy.h" } module rotate { private header "__algorithm/rotate.h" } diff --git a/libcxx/test/std/algorithms/algorithms.results/in_in_result.pass.cpp b/libcxx/test/std/algorithms/algorithms.results/in_in_result.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/std/algorithms/algorithms.results/in_in_result.pass.cpp @@ -0,0 +1,49 @@ +//===----------------------------------------------------------------------===// +// +// 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 + +// template +// struct in_in_result; + +#include +#include + +struct MoveOnly { + MoveOnly() = default; + MoveOnly(const MoveOnly&) = delete; + MoveOnly(MoveOnly&& o) { o.movedFrom = true; } + + bool movedFrom = false; +}; + +template +struct ConstructibleFrom { + ConstructibleFrom(T c) : content{c} {} + T content; +}; + +struct S2 {}; + +int main(int, char**) { + { + std::ranges::in_in_result res{10L, 0.}; + assert(res.in1 == 10); + assert(res.in2 == 0.); + auto res2 = static_cast, ConstructibleFrom>>(res); + assert(res2.in1.content == 10); + assert(res2.in2.content == 0.); + } + { + std::ranges::in_in_result res; + static_assert(sizeof(res) == sizeof(MoveOnly)); + assert(!res.in1.movedFrom); + [[maybe_unused]] auto res2 = static_cast>(std::move(res)); + assert(res.in1.movedFrom); + } +}