diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog index ea3efcc7767..901e7b8f2d0 100644 --- a/libstdc++-v3/ChangeLog +++ b/libstdc++-v3/ChangeLog @@ -1,3 +1,10 @@ +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. + 2011-04-25 Paolo Carlini * include/std/type_traits (struct underlying_type): Add. diff --git a/libstdc++-v3/include/bits/move.h b/libstdc++-v3/include/bits/move.h index f5918999cf9..a6dd2378eda 100644 --- a/libstdc++-v3/include/bits/move.h +++ b/libstdc++-v3/include/bits/move.h @@ -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 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /** * @brief Move a value. - * @ingroup mutating_algorithms + * @ingroup utilities * @param __t A thing of arbitrary type. * @return Same, moved. */ @@ -82,12 +82,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION 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 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION /** * @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. diff --git a/libstdc++-v3/testsuite/20_util/move_if_noexcept/1.cc b/libstdc++-v3/testsuite/20_util/move_if_noexcept/1.cc new file mode 100644 index 00000000000..f7169b0e85e --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/move_if_noexcept/1.cc @@ -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; +} diff --git a/libstdc++-v3/testsuite/20_util/move_if_noexcept/requirements/explicit_instantiation.cc b/libstdc++-v3/testsuite/20_util/move_if_noexcept/requirements/explicit_instantiation.cc new file mode 100644 index 00000000000..37ea6d6ef98 --- /dev/null +++ b/libstdc++-v3/testsuite/20_util/move_if_noexcept/requirements/explicit_instantiation.cc @@ -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&); +}