public inbox for libstdc++-cvs@sourceware.org help / color / mirror / Atom feed
From: Jonathan Wakely <redi@gcc.gnu.org> To: gcc-cvs@gcc.gnu.org, libstdc++-cvs@gcc.gnu.org Subject: [gcc r14-2104] libstdc++: Implement P2538R1 ADL-proof std::projected Date: Mon, 26 Jun 2023 16:43:33 +0000 (GMT) [thread overview] Message-ID: <20230626164333.B146A3858298@sourceware.org> (raw) https://gcc.gnu.org/g:6eafdfc73c21d7a5e59e18c9dee275af5bf6d979 commit r14-2104-g6eafdfc73c21d7a5e59e18c9dee275af5bf6d979 Author: Jonathan Wakely <jwakely@redhat.com> 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<projected<Iter, Proj>>): Remove. * testsuite/24_iterators/indirect_callable/projected-adl.cc: New test. 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 1555c374870..6802582a459 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<typename _Iter, typename _Proj> + struct __projected + { + struct __type + { + using value_type = remove_cvref_t<indirect_result_t<_Proj&, _Iter>>; + indirect_result_t<_Proj&, _Iter> operator*() const; // not defined + }; + }; + + template<weakly_incrementable _Iter, typename _Proj> + struct __projected<_Iter, _Proj> + { + struct __type + { + using value_type = remove_cvref_t<indirect_result_t<_Proj&, _Iter>>; + using difference_type = iter_difference_t<_Iter>; + indirect_result_t<_Proj&, _Iter> operator*() const; // not defined + }; + }; + } // namespace __detail + /// [projected], projected template<indirectly_readable _Iter, indirectly_regular_unary_invocable<_Iter> _Proj> - struct projected - { - using value_type = remove_cvref_t<indirect_result_t<_Proj&, _Iter>>; - - indirect_result_t<_Proj&, _Iter> operator*() const; // not defined - }; - - template<weakly_incrementable _Iter, typename _Proj> - struct incrementable_traits<projected<_Iter, _Proj>> - { using difference_type = iter_difference_t<_Iter>; }; + using projected = __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 <iterator> + +template<typename T> + concept has_diff_type = requires { typename T::difference_type; }; + +static_assert( has_diff_type<std::projected<int*, void(*)(int)>> ); + +struct Indy { + using value_type = int; + int operator*() const { return 0; } +}; +static_assert( ! std::weakly_incrementable<Indy> ); +static_assert( ! has_diff_type<std::projected<Indy, void(*)(int)>> ); + + +// Examples from the paper: + +template<class T> struct Holder { T t; }; +struct Incomplete; + +void test_concepts() +{ + using T = Holder<Incomplete>*; + static_assert(std::equality_comparable<T>); + (void) std::indirectly_comparable<T*, T*, std::equal_to<>>; + (void) std::sortable<T*>; +} + +#include <algorithm> + +void test_count() +{ + Holder<Incomplete>* a = nullptr; + (void) std::count(&a, &a, nullptr); + (void) std::ranges::count(&a, &a, nullptr); // { dg-bogus "." } +}
reply other threads:[~2023-06-26 16:43 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=20230626164333.B146A3858298@sourceware.org \ --to=redi@gcc.gnu.org \ --cc=gcc-cvs@gcc.gnu.org \ --cc=libstdc++-cvs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).