diff options
| author | Eric Fiselier <eric@efcs.ca> | 2016-09-26 20:55:02 +0000 |
|---|---|---|
| committer | Eric Fiselier <eric@efcs.ca> | 2016-09-26 20:55:02 +0000 |
| commit | c24e6dd3c8a0930b921388e9ce175afd5330eeaf (patch) | |
| tree | 52504002f3ef9a262b4f4304aa7bc372ec735304 /libcxx/include | |
| parent | fedd367d70f01def244efcb7f361b14a97908c5f (diff) | |
| download | bcm5719-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/include')
| -rw-r--r-- | libcxx/include/type_traits | 8 |
1 files changed, 4 insertions, 4 deletions
diff --git a/libcxx/include/type_traits b/libcxx/include/type_traits index c777f451ec7..26165713a24 100644 --- a/libcxx/include/type_traits +++ b/libcxx/include/type_traits @@ -2155,7 +2155,7 @@ template <class _Tp> _LIBCPP_CONSTEXPR bool is_destructible_v #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES template <class _Tp> -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR typename remove_reference<_Tp>::type&& move(_Tp&& __t) _NOEXCEPT { @@ -2164,7 +2164,7 @@ move(_Tp&& __t) _NOEXCEPT } template <class _Tp> -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR _Tp&& forward(typename remove_reference<_Tp>::type& __t) _NOEXCEPT { @@ -2172,12 +2172,12 @@ forward(typename remove_reference<_Tp>::type& __t) _NOEXCEPT } template <class _Tp> -inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11 +inline _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR _Tp&& forward(typename remove_reference<_Tp>::type&& __t) _NOEXCEPT { static_assert(!is_lvalue_reference<_Tp>::value, - "Can not forward an rvalue as an lvalue."); + "can not forward an rvalue as an lvalue"); return static_cast<_Tp&&>(__t); } |

