Hi, tested x86_64-linux, committed to mainline.
Thanks, Paolo. ///////////////////
2011-04-27 Paolo Carlini <paolo.carl...@oracle.com> * 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<typename std::remove_reference<_Tp>::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<typename _Tp> + 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 <paolo.carl...@oracle.com> + +// 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 +// <http://www.gnu.org/licenses/>. + +// NB: This file is for testing utility with NO OTHER INCLUDES. + +#include <utility> + +namespace std +{ + typedef short test_type; + + template + std::conditional<(!std::is_nothrow_move_constructible<test_type>::value + && std::is_copy_constructible<test_type>::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 <paolo.carl...@oracle.com> +// +// 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 +// <http://www.gnu.org/licenses/>. + +#include <utility> +#include <testsuite_hooks.h> + +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; +}