From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2181) id 5AB0F385800E; Mon, 18 Mar 2024 14:08:30 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5AB0F385800E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1710770910; bh=FvXRPCCISrMwgu5imiq87vyjFNbbIotPBJeT9oO2uW8=; h=From:To:Subject:Date:From; b=V8chDI6SA7CFr1WYmxoA4jfA3WPUOwRWL4aczImfayUqYpib+SP68eYsx+a3sXzy0 uiGxn1QItTAjw6nN231YGCtcD1trsuRzH73oUWfj+s8jpjVIIDNEmajcQ73LxnEyY/ 12e0dda97GXwbg4C+CuowWuDRPyI+o9hwYfO4bgU= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jonathan Wakely To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r12-10279] libstdc++: Implement P2538R1 ADL-proof std::projected X-Act-Checkin: gcc X-Git-Author: Jonathan Wakely X-Git-Refname: refs/heads/releases/gcc-12 X-Git-Oldrev: afefe951acd8bae13be0e1b700262316254ce935 X-Git-Newrev: 2d604183c99ef0cea5d7540a7a466fb1d1453a58 Message-Id: <20240318140830.5AB0F385800E@sourceware.org> Date: Mon, 18 Mar 2024 14:08:30 +0000 (GMT) List-Id: https://gcc.gnu.org/g:2d604183c99ef0cea5d7540a7a466fb1d1453a58 commit r12-10279-g2d604183c99ef0cea5d7540a7a466fb1d1453a58 Author: Jonathan Wakely Date: Fri Jun 23 12:18:11 2023 +0100 libstdc++: Implement P2538R1 ADL-proof std::projected This was recently approved for C++26, but there's no harm in implementing it unconditionally for C++20 and C++23. As it says in the paper, it doesn't change the meaning of any valid code. It only enables things that were previously ill-formed for questionable reasons. libstdc++-v3/ChangeLog: * include/bits/iterator_concepts.h (projected): Replace class template with alias template denoting an ADL-proofed helper. (incremental_traits>): Remove. * testsuite/24_iterators/indirect_callable/projected-adl.cc: New test. (cherry picked from commit 6eafdfc73c21d7a5e59e18c9dee275af5bf6d979) Diff: --- libstdc++-v3/include/bits/iterator_concepts.h | 35 ++++++++++++------ .../indirect_callable/projected-adl.cc | 42 ++++++++++++++++++++++ 2 files changed, 67 insertions(+), 10 deletions(-) diff --git a/libstdc++-v3/include/bits/iterator_concepts.h b/libstdc++-v3/include/bits/iterator_concepts.h index cf66c63f395..6e032b08eb7 100644 --- a/libstdc++-v3/include/bits/iterator_concepts.h +++ b/libstdc++-v3/include/bits/iterator_concepts.h @@ -771,19 +771,34 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION && invocable<_Fn, iter_reference_t<_Is>...> using indirect_result_t = invoke_result_t<_Fn, iter_reference_t<_Is>...>; + namespace __detail + { + template + struct __projected + { + struct __type + { + using value_type = remove_cvref_t>; + indirect_result_t<_Proj&, _Iter> operator*() const; // not defined + }; + }; + + template + struct __projected<_Iter, _Proj> + { + struct __type + { + using value_type = remove_cvref_t>; + using difference_type = iter_difference_t<_Iter>; + indirect_result_t<_Proj&, _Iter> operator*() const; // not defined + }; + }; + } // namespace __detail + /// [projected], projected template _Proj> - struct projected - { - using value_type = remove_cvref_t>; - - indirect_result_t<_Proj&, _Iter> operator*() const; // not defined - }; - - template - struct incrementable_traits> - { using difference_type = iter_difference_t<_Iter>; }; + using projected = typename __detail::__projected<_Iter, _Proj>::__type; // [alg.req], common algorithm requirements diff --git a/libstdc++-v3/testsuite/24_iterators/indirect_callable/projected-adl.cc b/libstdc++-v3/testsuite/24_iterators/indirect_callable/projected-adl.cc new file mode 100644 index 00000000000..4c2a0955c6e --- /dev/null +++ b/libstdc++-v3/testsuite/24_iterators/indirect_callable/projected-adl.cc @@ -0,0 +1,42 @@ +// { dg-options "-std=gnu++20" } +// { dg-do compile { target c++20 } } + +// P2538R1 ADL-proof std::projected +// https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2538r1.html + +#include + +template + concept has_diff_type = requires { typename T::difference_type; }; + +static_assert( has_diff_type> ); + +struct Indy { + using value_type = int; + int operator*() const { return 0; } +}; +static_assert( ! std::weakly_incrementable ); +static_assert( ! has_diff_type> ); + + +// Examples from the paper: + +template struct Holder { T t; }; +struct Incomplete; + +void test_concepts() +{ + using T = Holder*; + static_assert(std::equality_comparable); + (void) std::indirectly_comparable>; + (void) std::sortable; +} + +#include + +void test_count() +{ + Holder* a = nullptr; + (void) std::count(&a, &a, nullptr); + (void) std::ranges::count(&a, &a, nullptr); // { dg-bogus "." } +}