From patchwork Wed Nov 12 00:16:25 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jonathan Wakely X-Patchwork-Id: 409718 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id EF05F14012F for ; Wed, 12 Nov 2014 11:16:38 +1100 (AEDT) DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; q=dns; s= default; b=nTzUJEBbyVPZE9nKoC+hG7WOSC2HOfH+xmDVIj4gUNsWU8WfNHyL5 lwyyH+I4zq6nm4IhWQ24G2XAFo1q3orIF1OJ4crrvptqFLLfiq9krbOEVDB9t4Sd bZlE9eiTZL4LRvZf0/fOqw1ojSJkRR6tm2E8d4hotAdPUlHKuqoRDM= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:subject:message-id:mime-version:content-type; s= default; bh=JkSNayPEgeWoLvMfguK+4HAQLtM=; b=beIdOfpfuA6j7ORblmvi g4mQn71PVU3HMcqrrQULQHS/PLFdQJzb76tEjmR8AiFOUkgYKyKK8HgOFx4VJjEt Za3A5FsxReXslerWO1T0c5cVOfE5nx9wokxrEvZHmNJ2ou1FKKB41O8IPSuTGgH7 BD7klIwyhwYLipkh2R9vDdw= Received: (qmail 22243 invoked by alias); 12 Nov 2014 00:16:30 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 22221 invoked by uid 89); 12 Nov 2014 00:16:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-2.4 required=5.0 tests=AWL, BAYES_00, RP_MATCHES_RCVD, SPF_HELO_PASS, SPF_PASS autolearn=ham version=3.3.2 X-Spam-User: qpsmtpd, 2 recipients X-HELO: mx1.redhat.com Received: from mx1.redhat.com (HELO mx1.redhat.com) (209.132.183.28) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-GCM-SHA384 encrypted) ESMTPS; Wed, 12 Nov 2014 00:16:28 +0000 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id sAC0GQcB020196 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 11 Nov 2014 19:16:26 -0500 Received: from localhost (ovpn-116-136.ams2.redhat.com [10.36.116.136]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id sAC0GPEB011862; Tue, 11 Nov 2014 19:16:26 -0500 Date: Wed, 12 Nov 2014 00:16:25 +0000 From: Jonathan Wakely To: libstdc++@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [patch] Detect non-member operator&() overloads in std::experimental::optional Message-ID: <20141112001625.GQ5191@redhat.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) The _Has_addressof trait only detected an overloaded operator&() as a member function, this also checks for a non-member. Tested x86_64-linux, committed to trunk. commit 32b294e3eddf7789c25f3b751ae6b2a2c5c43275 Author: Jonathan Wakely Date: Mon Nov 10 19:02:02 2014 +0000 * include/experimental/optional (_Has_addressof): Check for non-member operator&. * testsuite/experimental/optional/observers/2.cc: Add operator&. * testsuite/experimental/optional/constexpr/observers/2.cc: Likewise. diff --git a/libstdc++-v3/include/experimental/optional b/libstdc++-v3/include/experimental/optional index 7e01abe..2d3127a 100644 --- a/libstdc++-v3/include/experimental/optional +++ b/libstdc++-v3/include/experimental/optional @@ -127,13 +127,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION __throw_bad_optional_access(const char* __s) { _GLIBCXX_THROW_OR_ABORT(bad_optional_access(__s)); } - template - struct _Has_addressof_impl : std::false_type { }; + template + struct _Has_addressof_mem : std::false_type { }; template - struct _Has_addressof_impl<_Tp, - decltype( std::declval().operator&(), void() )> - : std::true_type { }; + struct _Has_addressof_mem<_Tp, + __void_t().operator&() )> + > + : std::true_type { }; + + template + struct _Has_addressof_free : std::false_type { }; + + template + struct _Has_addressof_free<_Tp, + __void_t()) )> + > + : std::true_type { }; /** * @brief Trait that detects the presence of an overloaded unary operator&. @@ -143,7 +153,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION * declval<_Tp * const&>().operator&()). */ template - struct _Has_addressof : _Has_addressof_impl<_Tp>::type { }; + struct _Has_addressof + : std::__or_<_Has_addressof_mem<_Tp>, _Has_addressof_free<_Tp>>::type + { }; /** * @brief An overload that attempts to take the address of an lvalue as a diff --git a/libstdc++-v3/testsuite/experimental/optional/constexpr/observers/2.cc b/libstdc++-v3/testsuite/experimental/optional/constexpr/observers/2.cc index 14b5e3b..2c9215c 100644 --- a/libstdc++-v3/testsuite/experimental/optional/constexpr/observers/2.cc +++ b/libstdc++-v3/testsuite/experimental/optional/constexpr/observers/2.cc @@ -26,6 +26,8 @@ struct value_type { int i; + + void* operator&() { return nullptr; } // N.B. non-const }; int main() diff --git a/libstdc++-v3/testsuite/experimental/optional/observers/2.cc b/libstdc++-v3/testsuite/experimental/optional/observers/2.cc index 8499b47..9fb2edb 100644 --- a/libstdc++-v3/testsuite/experimental/optional/observers/2.cc +++ b/libstdc++-v3/testsuite/experimental/optional/observers/2.cc @@ -26,6 +26,8 @@ struct value_type int i; }; +void* operator&(const value_type&) = delete; + int main() { std::experimental::optional o { value_type { 51 } };