From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 5F098385773F for ; Mon, 26 Jun 2023 16:43:56 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5F098385773F Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1687797836; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=A4NXH2nt+G4BDGb/eu3C/xrs3xzT+o6piHc9/jwiolE=; b=NnM5YTl5e8yhJOWOlAXUv8lXUKEhcQiflzBxHe0TU+xwZuN6C9amQ1VDN7Q27uW/DRIcDv yKapz2LoWPmUE9D+ko8XkZR4ZILE5AUBr5mzCMd8KbM0jye4zfe4E/7cZwCqTo31V/M+WH 4IoW8lvN7owcBtq6gu8tFKEeNGmEobE= Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-499-PiDoqz2TM4KkhWen4kxIpQ-1; Mon, 26 Jun 2023 12:43:54 -0400 X-MC-Unique: PiDoqz2TM4KkhWen4kxIpQ-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 7E2191C0690D; Mon, 26 Jun 2023 16:43:51 +0000 (UTC) Received: from localhost (unknown [10.42.28.110]) by smtp.corp.redhat.com (Postfix) with ESMTP id 41516C1ED97; Mon, 26 Jun 2023 16:43:51 +0000 (UTC) From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Implement P2538R1 ADL-proof std::projected Date: Mon, 26 Jun 2023 17:43:46 +0100 Message-ID: <20230626164350.270495-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Tested x86_64-linux. Pushed to trunk. -- >8 -- 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. --- libstdc++-v3/include/bits/iterator_concepts.h | 35 +++++++++++----- .../indirect_callable/projected-adl.cc | 42 +++++++++++++++++++ 2 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 libstdc++-v3/testsuite/24_iterators/indirect_callable/projected-adl.cc 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 + 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 = __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 "." } +} -- 2.41.0