mirror of
https://github.com/llvm-mirror/libcxx.git
synced 2025-10-23 01:18:52 +08:00

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 git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@282439 91177308-0d34-0410-b5e6-96231b3b80d8
92 lines
3.1 KiB
C++
92 lines
3.1 KiB
C++
//===----------------------------------------------------------------------===//
|
|
//
|
|
// 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.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// UNSUPPORTED: c++98, c++03
|
|
|
|
// test forward
|
|
|
|
#include <utility>
|
|
#include <type_traits>
|
|
#include <cassert>
|
|
|
|
#include "test_macros.h"
|
|
|
|
struct A
|
|
{
|
|
};
|
|
|
|
A source() noexcept {return A();}
|
|
const A csource() noexcept {return A();}
|
|
|
|
|
|
constexpr bool test_constexpr_forward() {
|
|
#if TEST_STD_VER > 11
|
|
int x = 42;
|
|
const int cx = 101;
|
|
return std::forward<int&>(x) == 42
|
|
&& std::forward<int>(x) == 42
|
|
&& std::forward<const int&>(x) == 42
|
|
&& std::forward<const int>(x) == 42
|
|
&& std::forward<int&&>(x) == 42
|
|
&& std::forward<const int&&>(x) == 42
|
|
&& std::forward<const int&>(cx) == 101
|
|
&& std::forward<const int>(cx) == 101;
|
|
#else
|
|
return true;
|
|
#endif
|
|
}
|
|
|
|
int main()
|
|
{
|
|
A a;
|
|
const A ca = A();
|
|
|
|
((void)a); // Prevent unused warning
|
|
((void)ca); // Prevent unused warning
|
|
|
|
static_assert(std::is_same<decltype(std::forward<A&>(a)), A&>::value, "");
|
|
static_assert(std::is_same<decltype(std::forward<A>(a)), A&&>::value, "");
|
|
static_assert(std::is_same<decltype(std::forward<A>(source())), A&&>::value, "");
|
|
static_assert(noexcept(std::forward<A&>(a)), "");
|
|
static_assert(noexcept(std::forward<A>(a)), "");
|
|
static_assert(noexcept(std::forward<A>(source())), "");
|
|
|
|
static_assert(std::is_same<decltype(std::forward<const A&>(a)), const A&>::value, "");
|
|
static_assert(std::is_same<decltype(std::forward<const A>(a)), const A&&>::value, "");
|
|
static_assert(std::is_same<decltype(std::forward<const A>(source())), const A&&>::value, "");
|
|
static_assert(noexcept(std::forward<const A&>(a)), "");
|
|
static_assert(noexcept(std::forward<const A>(a)), "");
|
|
static_assert(noexcept(std::forward<const A>(source())), "");
|
|
|
|
static_assert(std::is_same<decltype(std::forward<const A&>(ca)), const A&>::value, "");
|
|
static_assert(std::is_same<decltype(std::forward<const A>(ca)), const A&&>::value, "");
|
|
static_assert(std::is_same<decltype(std::forward<const A>(csource())), const A&&>::value, "");
|
|
static_assert(noexcept(std::forward<const A&>(ca)), "");
|
|
static_assert(noexcept(std::forward<const A>(ca)), "");
|
|
static_assert(noexcept(std::forward<const A>(csource())), "");
|
|
|
|
#if TEST_STD_VER > 11
|
|
{
|
|
constexpr int i2 = std::forward<int>(42);
|
|
static_assert(std::forward<int>(42) == 42, "");
|
|
static_assert(std::forward<const int&>(i2) == 42, "");
|
|
static_assert(test_constexpr_forward(), "");
|
|
}
|
|
#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 i2 = std::forward<int>(42);
|
|
static_assert(std::forward<int>(42) == 42, "" );
|
|
static_assert(std::forward<const int&>(i2) == 42, "");
|
|
}
|
|
#endif
|
|
}
|