From patchwork Fri Oct 22 22:11:18 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 46559 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 809AB385781C for ; Fri, 22 Oct 2021 22:11:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 809AB385781C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634940711; bh=a6Xj0C0ojx+abw36A0MvSPs5ieAePMb50UPzz6QLjHw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=wYJCSRd3eRTfAw76ZTv+iJDlQMiFZxs25G1en7hejeuDGQiUHc1ksfOyx/uTA6BXF Cs/HJnETkZ8uoXOSyqwOlx9JEIvofuHLMcweugjkyX6okOJQBEgUqupt59qKfLb1Of FMfKjK9Oo8iu8hWTCWFwOgBz48hAZW6LBbG32Be4= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org 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 C01993858416 for ; Fri, 22 Oct 2021 22:11:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C01993858416 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-53-0JXYFGgAOmak07IcCh-LBg-1; Fri, 22 Oct 2021 18:11:20 -0400 X-MC-Unique: 0JXYFGgAOmak07IcCh-LBg-1 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 8FB7B1006AA2; Fri, 22 Oct 2021 22:11:19 +0000 (UTC) Received: from localhost (unknown [10.33.36.194]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2FBC560854; Fri, 22 Oct 2021 22:11:18 +0000 (UTC) To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [committed] libstdc++: Constrain std::make_any [PR102894] Date: Fri, 22 Oct 2021 23:11:18 +0100 Message-Id: <20211022221118.958340-1-jwakely@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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_LOW, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, TXREP, URI_HEX autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jonathan Wakely via Gcc-patches From: Jonathan Wakely Reply-To: Jonathan Wakely Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" std::make_any should be constrained so it can only be called if the construction of the return value would be valid. Tested x86_64-linux, committed to trunk. I plan to backport this too. libstdc++-v3/ChangeLog: PR libstdc++/102894 * include/std/any (make_any): Add SFINAE constraint. * testsuite/20_util/any/102894.cc: New test. --- libstdc++-v3/include/std/any | 13 +++++++++---- libstdc++-v3/testsuite/20_util/any/102894.cc | 20 ++++++++++++++++++++ 2 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 libstdc++-v3/testsuite/20_util/any/102894.cc diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any index 9c102a58b26..f75dddf6d92 100644 --- a/libstdc++-v3/include/std/any +++ b/libstdc++-v3/include/std/any @@ -428,16 +428,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /// Exchange the states of two @c any objects. inline void swap(any& __x, any& __y) noexcept { __x.swap(__y); } - /// Create an any holding a @c _Tp constructed from @c __args. + /// Create an `any` holding a `_Tp` constructed from `__args...`. template - any make_any(_Args&&... __args) + inline + enable_if_t, _Args...>, any> + make_any(_Args&&... __args) { return any(in_place_type<_Tp>, std::forward<_Args>(__args)...); } - /// Create an any holding a @c _Tp constructed from @c __il and @c __args. + /// Create an `any` holding a `_Tp` constructed from `__il` and `__args...`. template - any make_any(initializer_list<_Up> __il, _Args&&... __args) + inline + enable_if_t, + initializer_list<_Up>&, _Args...>, any> + make_any(initializer_list<_Up> __il, _Args&&... __args) { return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...); } diff --git a/libstdc++-v3/testsuite/20_util/any/102894.cc b/libstdc++-v3/testsuite/20_util/any/102894.cc new file mode 100644 index 00000000000..66ea9a03fea --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/any/102894.cc @@ -0,0 +1,20 @@ +// { dg-do compile { target c++17 } } +#include + +template +struct can_make_any +: std::false_type +{ }; + +template +struct can_make_any())>> +: std::true_type +{ }; + +struct move_only +{ + move_only() = default; + move_only(move_only&&) = default; +}; + +static_assert( ! can_make_any::value ); // PR libstdc++/102894