From patchwork Thu Dec 15 20:33:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ville Voutilainen X-Patchwork-Id: 706251 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 3tflXm1F7qz9snm for ; Fri, 16 Dec 2016 07:34:04 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b="G736KwQc"; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender :mime-version:from:date:message-id:subject:to:content-type; q= dns; s=default; b=qzay4HWm6vbbBMtgzFA/WInygUGy0QQ+SPFvEqk7iO0BqR sM1uGV8nJaBIszh+mKPkVC8ANZ7Suyu+lmBgOH1HB3Ko5JkpRyoW2Q8LRTDnKosV 7nX2JXneYa65tfFHbuCShH6WM2rbfR6FY3a12y7HCD3rYxwiS0NFoMYhgOas4= 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 :mime-version:from:date:message-id:subject:to:content-type; s= default; bh=rN40/u+mIpVO60e7tDHGVIuYqLo=; b=G736KwQcP3jz8DWF8Cfc 7yFe6G3ivsAgP9kVjZE/3TQIg6dx4JnpRPA1omqRmAbI3nnNAQneDXKiZP02X1cT uPjjE47cmdEWgi7Ok4FxvyZFN4f7B+4U0/02Xzqh+t89MPt3LxJ/snxEPvQierOc 1LE1T4j4fDA2qN+DsRWBt90= Received: (qmail 29377 invoked by alias); 15 Dec 2016 20:33:42 -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 29351 invoked by uid 89); 15 Dec 2016 20:33:42 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.6 required=5.0 tests=AWL, BAYES_05, FREEMAIL_FROM, RCVD_IN_DNSWL_NONE, SPF_PASS autolearn=ham version=3.3.2 spammy=Voutilainen, voutilainen, H*c:sk:94eb2c1, *__p X-Spam-User: qpsmtpd, 2 recipients X-HELO: mail-ua0-f170.google.com Received: from mail-ua0-f170.google.com (HELO mail-ua0-f170.google.com) (209.85.217.170) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Thu, 15 Dec 2016 20:33:31 +0000 Received: by mail-ua0-f170.google.com with SMTP id 51so18460004uai.1; Thu, 15 Dec 2016 12:33:31 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=dOAKHwOkr8AzoFKgcpjx4+ujjx9b4JllMctOy48ezqY=; b=GPbenaZAkbp4zYKeIgI1k4rsrfxAqPPbpO3wA9em9JUOAt6N51NhSJG5VycKv8xj+d lreqUU2PkT6yVZfHLpcR5LuEURnZGXonOYt5hxNFVLvqrHAHxvkZzPLEnYiXP7PZkTj+ s+ur0J9bdkDnWToKcK6a6uVfvxO1jbcvOIiY5OAF43mUXJhEkhllqVXM8m4nio9wl4Li YzIbgrNMGBkhBhFhPy+QWfUVoJQVjoxxfjh75Ei90KAalRHEAyD4US9YEM3yPT8kLkFE tS/+HTBRVfl0LbtQrhcrtfDcD4U9o9J+PzSLB/VfD21+yrgDtwgwqJM1uDhRxmx1ItMs 844Q== X-Gm-Message-State: AKaTC00bWc0b61BKOCd2qNG95mFn7NbEgX5cktIeuhdlhMjNfzqeGnGVr/nQlDrSZGKPvpwvsPK4+HP/F/1bbA== X-Received: by 10.176.65.4 with SMTP id j4mr4128291uad.93.1481834009258; Thu, 15 Dec 2016 12:33:29 -0800 (PST) MIME-Version: 1.0 Received: by 10.103.50.74 with HTTP; Thu, 15 Dec 2016 12:33:28 -0800 (PST) From: Ville Voutilainen Date: Thu, 15 Dec 2016 22:33:28 +0200 Message-ID: Subject: [v3 PATCH] Implement LWG 2769, Redundant const in the return type of any_cast(const any&). To: "libstdc++" , "gcc-patches@gcc.gnu.org" Tested on Linux-x64. 2016-12-15 Ville Voutilainen Implement LWG 2769, Redundant const in the return type of any_cast(const any&). * include/std/any (_AnyCast): New. (any_cast(const any&)): Use it and add an explicit cast for return. (any_cast(any&)): Likewise. (any_cast(any&&)): Likewise. * testsuite/20_util/any/misc/any_cast.cc: Add a test for a type that has an explicit copy constructor. *testsuite/20_util/any/misc/any_cast_neg.cc: Adjust. diff --git a/libstdc++-v3/include/std/any b/libstdc++-v3/include/std/any index ded2bb2..820427c 100644 --- a/libstdc++-v3/include/std/any +++ b/libstdc++-v3/include/std/any @@ -433,6 +433,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION return any(in_place_type<_Tp>, __il, std::forward<_Args>(__args)...); } + template + using _AnyCast = remove_cv_t>; /** * @brief Access the contained object. * @@ -448,9 +450,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { static_assert(any::__is_valid_cast<_ValueType>(), "Template argument must be a reference or CopyConstructible type"); - auto __p = any_cast>>(&__any); + auto __p = any_cast<_AnyCast<_ValueType>>(&__any); if (__p) - return *__p; + return static_cast<_ValueType>(*__p); __throw_bad_any_cast(); } @@ -471,9 +473,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { static_assert(any::__is_valid_cast<_ValueType>(), "Template argument must be a reference or CopyConstructible type"); - auto __p = any_cast>(&__any); + auto __p = any_cast<_AnyCast<_ValueType>>(&__any); if (__p) - return *__p; + return static_cast<_ValueType>(*__p); __throw_bad_any_cast(); } @@ -485,9 +487,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { static_assert(any::__is_valid_cast<_ValueType>(), "Template argument must be a reference or CopyConstructible type"); - auto __p = any_cast>(&__any); + auto __p = any_cast<_AnyCast<_ValueType>>(&__any); if (__p) - return *__p; + return static_cast<_ValueType>(*__p); __throw_bad_any_cast(); } @@ -499,9 +501,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION { static_assert(any::__is_valid_cast<_ValueType>(), "Template argument must be a reference or CopyConstructible type"); - auto __p = any_cast>(&__any); + using _Up = remove_cv_t>; + auto __p = any_cast<_AnyCast<_ValueType>>(&__any); if (__p) - return std::move(*__p); + return static_cast<_ValueType>(std::move(*__p)); __throw_bad_any_cast(); } // @} diff --git a/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc b/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc index 96f9419..f3ae592 100644 --- a/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc +++ b/libstdc++-v3/testsuite/20_util/any/misc/any_cast.cc @@ -106,9 +106,22 @@ void test03() MoveDeleted&& md3 = any_cast(any(std::move(md))); } +void test04() +{ + struct ExplicitCopy + { + ExplicitCopy() = default; + explicit ExplicitCopy(const ExplicitCopy&) = default; + }; + any x = ExplicitCopy(); + ExplicitCopy ec{any_cast(x)}; + ExplicitCopy ec2{any_cast(std::move(x))}; +} + int main() { test01(); test02(); test03(); + test04(); } diff --git a/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc b/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc index 4de400d..a8a1ca9 100644 --- a/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc +++ b/libstdc++-v3/testsuite/20_util/any/misc/any_cast_neg.cc @@ -26,5 +26,5 @@ void test01() using std::any_cast; const any y(1); - any_cast(y); // { dg-error "qualifiers" "" { target { *-*-* } } 453 } + any_cast(y); // { dg-error "invalid static_cast" "" { target { *-*-* } } 455 } }