summaryrefslogtreecommitdiffstats
path: root/libcxx/test/std/utilities/utility/forward/move.pass.cpp
diff options
context:
space:
mode:
authorEric Fiselier <eric@efcs.ca>2016-09-26 20:55:02 +0000
committerEric Fiselier <eric@efcs.ca>2016-09-26 20:55:02 +0000
commitc24e6dd3c8a0930b921388e9ce175afd5330eeaf (patch)
tree52504002f3ef9a262b4f4304aa7bc372ec735304 /libcxx/test/std/utilities/utility/forward/move.pass.cpp
parentfedd367d70f01def244efcb7f361b14a97908c5f (diff)
downloadbcm5719-llvm-c24e6dd3c8a0930b921388e9ce175afd5330eeaf.tar.gz
bcm5719-llvm-c24e6dd3c8a0930b921388e9ce175afd5330eeaf.zip
[libc++] Extension: Make `move` and `forward` constexpr in C++11.
Summary: `std::move` and `std::forward` were not marked constexpr in C++11. This can be very damaging because it makes otherwise constant expressions non-constant. For example: ``` #include <utility> template <class T> struct Foo { constexpr Foo(T&& tx) : t(std::move(tx)) {} T t; }; [[clang::require_constant_initialization]] Foo<int> f(42); // Foo should be constant initialized but C++11 move is not constexpr. As a result `f` is an unsafe global. ``` This patch applies `constexpr` to `move` and `forward` as an extension in C++11. Normally the library is not allowed to add `constexpr` because it may be observable to the user. In particular adding constexpr may cause valid code to stop compiling. However these problems only happen in more complex situations, like making `__invoke(...)` constexpr. `forward` and `move` are simply enough that applying `constexpr` is safe. Note that libstdc++ has offered this extension since at least 4.8.1. Most of the changes in this patch are simply test cleanups or additions. The main changes in the tests are: * Fold all `forward_N.fail.cpp` tests into a single `forward.fail.cpp` test using -verify. * Delete most `move_only_N.fail.cpp` tests because they weren't actually testing anything. * Fold `move_copy.pass.cpp` and `move_only.pass.cpp` into a single `move.pass.cpp` test. * Add return type and noexcept tests for `forward` and `move`. Reviewers: rsmith, mclow.lists, EricWF Subscribers: K-ballo, loladiro Differential Revision: https://reviews.llvm.org/D24637 llvm-svn: 282439
Diffstat (limited to 'libcxx/test/std/utilities/utility/forward/move.pass.cpp')
-rw-r--r--libcxx/test/std/utilities/utility/forward/move.pass.cpp121
1 files changed, 121 insertions, 0 deletions
diff --git a/libcxx/test/std/utilities/utility/forward/move.pass.cpp b/libcxx/test/std/utilities/utility/forward/move.pass.cpp
new file mode 100644
index 00000000000..9dfebcc5cc2
--- /dev/null
+++ b/libcxx/test/std/utilities/utility/forward/move.pass.cpp
@@ -0,0 +1,121 @@
+//===----------------------------------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// test move
+
+// UNSUPPORTED: c++98, c++03
+
+#include <utility>
+#include <type_traits>
+#include <cassert>
+
+#include "test_macros.h"
+
+class move_only
+{
+ move_only(const move_only&);
+ move_only& operator=(const move_only&);
+public:
+ move_only(move_only&&) {}
+ move_only& operator=(move_only&&) {return *this;}
+
+ move_only() {}
+};
+
+move_only source() {return move_only();}
+const move_only csource() {return move_only();}
+
+void test(move_only) {}
+
+int x = 42;
+const int& cx = x;
+
+template <class QualInt>
+QualInt get() noexcept { return static_cast<QualInt>(x); }
+
+
+int copy_ctor = 0;
+int move_ctor = 0;
+
+struct A {
+ A() {}
+ A(const A&) {++copy_ctor;}
+ A(A&&) {++move_ctor;}
+ A& operator=(const A&) = delete;
+};
+
+constexpr bool test_constexpr_move() {
+#if TEST_STD_VER > 11
+ int x = 42;
+ const int cx = x;
+ return std::move(x) == 42
+ && std::move(cx) == 42
+ && std::move(static_cast<int&&>(x)) == 42
+ && std::move(static_cast<int const&&>(x)) == 42;
+#else
+ return true;
+#endif
+}
+
+int main()
+{
+ { // Test return type and noexcept.
+ static_assert(std::is_same<decltype(std::move(x)), int&&>::value, "");
+ static_assert(noexcept(std::move(x)), "");
+ static_assert(std::is_same<decltype(std::move(cx)), const int&&>::value, "");
+ static_assert(noexcept(std::move(cx)), "");
+ static_assert(std::is_same<decltype(std::move(42)), int&&>::value, "");
+ static_assert(noexcept(std::move(42)), "");
+ static_assert(std::is_same<decltype(std::move(get<const int&&>())), const int&&>::value, "");
+ static_assert(noexcept(std::move(get<int const&&>())), "");
+ }
+ { // test copy and move semantics
+ A a;
+ const A ca = A();
+
+ assert(copy_ctor == 0);
+ assert(move_ctor == 0);
+
+ A a2 = a;
+ assert(copy_ctor == 1);
+ assert(move_ctor == 0);
+
+ A a3 = std::move(a);
+ assert(copy_ctor == 1);
+ assert(move_ctor == 1);
+
+ A a4 = ca;
+ assert(copy_ctor == 2);
+ assert(move_ctor == 1);
+
+ A a5 = std::move(ca);
+ assert(copy_ctor == 3);
+ assert(move_ctor == 1);
+ }
+ { // test on a move only type
+ move_only mo;
+ test(std::move(mo));
+ test(source());
+ }
+#if TEST_STD_VER > 11
+ {
+ constexpr int x = 42;
+ static_assert(std::move(x) == 42, "");
+ static_assert(test_constexpr_move(), "");
+ }
+#endif
+#if TEST_STD_VER == 11 && defined(_LIBCPP_VERSION)
+ // Test that std::forward is constexpr in C++11. This is an extension
+ // provided by both libc++ and libstdc++.
+ {
+ constexpr int x = 42;
+ static_assert(std::move(x) == 42, "");
+ }
+#endif
+}
OpenPOWER on IntegriCloud