aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Carlini <paolo.carlini@oracle.com>2011-04-27 18:37:32 +0000
committerPaolo Carlini <paolo@gcc.gnu.org>2011-04-27 18:37:32 +0000
commit1f428429c431c09ff92b91820d38072b6d42b895 (patch)
tree75d9883f51e5234ed24a523323d1258cdeee6e0f
parentcbb734aa019ae7569549ba2cfa1887dd230f6d74 (diff)
downloadgcc-1f428429c431c09ff92b91820d38072b6d42b895.zip
gcc-1f428429c431c09ff92b91820d38072b6d42b895.tar.gz
gcc-1f428429c431c09ff92b91820d38072b6d42b895.tar.bz2
move.h (move_if_noexcept): Add.
2011-04-27 Paolo Carlini <paolo.carlini@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. From-SVN: r173044
-rw-r--r--libstdc++-v3/ChangeLog7
-rw-r--r--libstdc++-v3/include/bits/move.h21
-rw-r--r--libstdc++-v3/testsuite/20_util/move_if_noexcept/1.cc119
-rw-r--r--libstdc++-v3/testsuite/20_util/move_if_noexcept/requirements/explicit_instantiation.cc36
4 files changed, 180 insertions, 3 deletions
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index ea3efcc..901e7b8 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,10 @@
+2011-04-27 Paolo Carlini <paolo.carlini@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.
+
2011-04-25 Paolo Carlini <paolo.carlini@oracle.com>
* 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 f591899..a6dd237 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<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 @@ _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 0000000..f7169b0
--- /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 <paolo.carlini@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;
+}
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 0000000..37ea6d6
--- /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 <paolo.carlini@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&);
+}