From patchwork Wed Apr 27 18:38:14 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Carlini X-Patchwork-Id: 93109 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]) by ozlabs.org (Postfix) with SMTP id 3B3DDB6EEE for ; Thu, 28 Apr 2011 04:39:03 +1000 (EST) Received: (qmail 2840 invoked by alias); 27 Apr 2011 18:38:57 -0000 Received: (qmail 2824 invoked by uid 22791); 27 Apr 2011 18:38:55 -0000 X-SWARE-Spam-Status: No, hits=-1.1 required=5.0 tests=AWL, BAYES_00, RCVD_IN_BL_SPAMCOP_NET, RCVD_IN_DNSWL_NONE X-Spam-Check-By: sourceware.org Received: from smtp209.alice.it (HELO smtp209.alice.it) (82.57.200.105) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Wed, 27 Apr 2011 18:38:40 +0000 Received: from [192.168.1.4] (79.53.21.49) by smtp209.alice.it (8.5.124.08) id 4D498EF3073017B0; Wed, 27 Apr 2011 20:38:36 +0200 Message-ID: <4DB86296.8030208@oracle.com> Date: Wed, 27 Apr 2011 20:38:14 +0200 From: Paolo Carlini User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.14) Gecko/20110221 SUSE/3.1.8 Thunderbird/3.1.8 MIME-Version: 1.0 To: "gcc-patches@gcc.gnu.org" CC: libstdc++ Subject: [v3] Add move_if_noexcept X-IsSubscribed: yes 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 Hi, tested x86_64-linux, committed to mainline. Thanks, Paolo. /////////////////// 2011-04-27 Paolo Carlini * include/bits/move.h (move_if_noexcept): Add. * testsuite/20_util/move_if_noexcept/requirements/ explicit_instantiation.cc: New. * testsuite/20_util/move_if_noexcept/1.cc: Likewise. Index: include/bits/move.h =================================================================== --- include/bits/move.h (revision 173043) +++ include/bits/move.h (working copy) @@ -1,6 +1,6 @@ // Move, forward and identity for C++0x + swap -*- C++ -*- -// Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc. +// Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc. // // This file is part of the GNU ISO C++ Library. This library is free // software; you can redistribute it and/or modify it under the @@ -73,7 +73,7 @@ /** * @brief Move a value. - * @ingroup mutating_algorithms + * @ingroup utilities * @param __t A thing of arbitrary type. * @return Same, moved. */ @@ -82,12 +82,27 @@ move(_Tp&& __t) { return static_cast::type&&>(__t); } + /** + * @brief Move unless it could throw and the type is copyable. + * @ingroup utilities + * @param __x A thing of arbitrary type. + * @return Same, possibly moved. + */ + template + inline typename + conditional<(!is_nothrow_move_constructible<_Tp>::value + && is_copy_constructible<_Tp>::value), + const _Tp&, _Tp&&>::type + move_if_noexcept(_Tp& __x) noexcept + { return std::move(__x); } + /// declval, from type_traits. /** * @brief Returns the actual address of the object or function * referenced by r, even in the presence of an overloaded * operator&. + * @ingroup utilities * @param __r Reference to an object or function. * @return The actual address. */ @@ -112,7 +127,7 @@ /** * @brief Swaps two values. - * @ingroup mutating_algorithms + * @ingroup utilities * @param __a A thing of arbitrary type. * @param __b Another thing of arbitrary type. * @return Nothing. Index: testsuite/20_util/move_if_noexcept/requirements/explicit_instantiation.cc =================================================================== --- testsuite/20_util/move_if_noexcept/requirements/explicit_instantiation.cc (revision 0) +++ testsuite/20_util/move_if_noexcept/requirements/explicit_instantiation.cc (revision 0) @@ -0,0 +1,36 @@ +// { dg-options "-std=gnu++0x" } +// { dg-do compile } + +// 2011-04-27 Paolo Carlini + +// Copyright (C) 2011 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. + +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +// NB: This file is for testing utility with NO OTHER INCLUDES. + +#include + +namespace std +{ + typedef short test_type; + + template + std::conditional<(!std::is_nothrow_move_constructible::value + && std::is_copy_constructible::value), + const test_type&, test_type&&>::type + move_if_noexcept(test_type&); +} Index: testsuite/20_util/move_if_noexcept/1.cc =================================================================== --- testsuite/20_util/move_if_noexcept/1.cc (revision 0) +++ testsuite/20_util/move_if_noexcept/1.cc (revision 0) @@ -0,0 +1,119 @@ +// { dg-options "-std=gnu++0x" } + +// 2011-04-27 Paolo Carlini +// +// Copyright (C) 2011 Free Software Foundation, Inc. +// +// This file is part of the GNU ISO C++ Library. This library is free +// software; you can redistribute it and/or modify it under the +// terms of the GNU General Public License as published by the +// Free Software Foundation; either version 3, or (at your option) +// any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with this library; see the file COPYING3. If not see +// . + +#include +#include + +struct noexcept_move_copy +{ + noexcept_move_copy() + : status(true) + { }; + + noexcept_move_copy(noexcept_move_copy&& r) noexcept + { r.status = false; }; + + noexcept_move_copy(const noexcept_move_copy&) = default; + + operator bool() { return status; } + +private: + bool status; +}; + +struct noexcept_move_no_copy +{ + noexcept_move_no_copy() + : status(true) + { }; + + noexcept_move_no_copy(noexcept_move_no_copy&& r) noexcept + { r.status = false; }; + + noexcept_move_no_copy(const noexcept_move_no_copy&) = delete; + + operator bool() { return status; } + +private: + bool status; +}; + +struct except_move_copy +{ + except_move_copy() + : status(true) + { }; + + except_move_copy(except_move_copy&& r) noexcept(false) + { r.status = false; }; + + except_move_copy(const except_move_copy&) = default; + + operator bool() { return status; } + +private: + bool status; +}; + +struct except_move_no_copy +{ + except_move_no_copy() + : status(true) + { }; + + except_move_no_copy(except_move_no_copy&& r) noexcept(false) + { r.status = false; }; + + except_move_no_copy(const except_move_no_copy&) = delete; + + operator bool() { return status; } + +private: + bool status; +}; + +void +test01() +{ + bool test __attribute__((unused)) = true; + + noexcept_move_copy nemc1; + auto nemc2 __attribute__((unused)) = std::move_if_noexcept(nemc1); + VERIFY( nemc1 == false ); + + noexcept_move_no_copy nemnc1; + auto nemnc2 __attribute__((unused)) = std::move_if_noexcept(nemnc1); + VERIFY( nemnc1 == false ); + + except_move_copy emc1; + auto emc2 __attribute__((unused)) = std::move_if_noexcept(emc1); + VERIFY( emc1 == true ); + + except_move_no_copy emnc1; + auto emnc2 __attribute__((unused)) = std::move_if_noexcept(emnc1); + VERIFY( emnc1 == false ); +} + +int main() +{ + test01(); + return 0; +}