diff options
| author | Louis Dionne <ldionne@apple.com> | 2019-02-27 17:57:38 +0000 |
|---|---|---|
| committer | Louis Dionne <ldionne@apple.com> | 2019-02-27 17:57:38 +0000 |
| commit | ab19068b7f75cf86e330c51f71603a469b06e7bc (patch) | |
| tree | 3e235fa47e5eea0a3f8cb6745e51c308c8ea82a4 | |
| parent | 2dca65388c8d2ee8661e06e9cc9377ff47522281 (diff) | |
| download | bcm5719-llvm-ab19068b7f75cf86e330c51f71603a469b06e7bc.tar.gz bcm5719-llvm-ab19068b7f75cf86e330c51f71603a469b06e7bc.zip | |
[libc++] Add is_nothrow_convertible from P0758R1
Reviewed as https://reviews.llvm.org/D58019.
Thanks to Zoe Carver for the patch.
llvm-svn: 355010
| -rw-r--r-- | libcxx/include/type_traits | 29 | ||||
| -rw-r--r-- | libcxx/test/std/type_traits/is_nothrow_convertible.pass.cpp | 58 | ||||
| -rw-r--r-- | libcxx/www/cxx2a_status.html | 2 |
3 files changed, 88 insertions, 1 deletions
diff --git a/libcxx/include/type_traits b/libcxx/include/type_traits index 965ae7a29e6..cd67eb89e03 100644 --- a/libcxx/include/type_traits +++ b/libcxx/include/type_traits @@ -143,7 +143,10 @@ namespace std // Relationships between types: template <class T, class U> struct is_same; template <class Base, class Derived> struct is_base_of; + template <class From, class To> struct is_convertible; + template <typename From, typename To> struct is_nothrow_convertible; // C++20 + template <typename From, typename To> inline constexpr bool is_nothrow_convertible_v; // C++20 template <class Fn, class... ArgTypes> struct is_invocable; template <class R, class Fn, class... ArgTypes> struct is_invocable_r; @@ -1578,6 +1581,32 @@ _LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool is_convertible_v = is_convertible<_From, _To>::value; #endif +// is_nothrow_convertible + +#if _LIBCPP_STD_VER > 17 + +template <typename _Tp> +static void __test_noexcept(_Tp) noexcept; + +template<typename _Fm, typename _To> +static bool_constant<noexcept(__test_noexcept<_To>(declval<_Fm>()))> +__is_nothrow_convertible_test(); + +template <typename _Fm, typename _To> +struct __is_nothrow_convertible_helper: decltype(__is_nothrow_convertible_test<_Fm, _To>()) +{ }; + +template <typename _Fm, typename _To> +struct is_nothrow_convertible : __or_< + __and_<is_void<_To>, is_void<_Fm>>, + __and_<is_convertible<_Fm, _To>, __is_nothrow_convertible_helper<_Fm, _To>> +>::type { }; + +template <typename _Fm, typename _To> +inline constexpr bool is_nothrow_convertible_v = is_nothrow_convertible<_Fm, _To>::value; + +#endif // _LIBCPP_STD_VER > 17 + // is_empty #if __has_feature(is_empty) || (_GNUC_VER >= 407) diff --git a/libcxx/test/std/type_traits/is_nothrow_convertible.pass.cpp b/libcxx/test/std/type_traits/is_nothrow_convertible.pass.cpp new file mode 100644 index 00000000000..2a953d90b8e --- /dev/null +++ b/libcxx/test/std/type_traits/is_nothrow_convertible.pass.cpp @@ -0,0 +1,58 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// + +// <type_traits> +// UNSUPPORTED: c++98, c++03, c++11, c++14, c++17 + +#include <type_traits> + +struct A {}; +struct B { +public: + operator A() { return a; } A a; +}; + +class C { }; +class D { +public: + operator C() noexcept { return c; } C c; +}; + +int main(int, char**) { + static_assert((std::is_nothrow_convertible<int, double>::value), ""); + static_assert(!(std::is_nothrow_convertible<int, char*>::value), ""); + + static_assert(!(std::is_nothrow_convertible<A, B>::value), ""); + static_assert((std::is_nothrow_convertible<D, C>::value), ""); + + static_assert((std::is_nothrow_convertible_v<int, double>), ""); + static_assert(!(std::is_nothrow_convertible_v<int, char*>), ""); + + static_assert(!(std::is_nothrow_convertible_v<A, B>), ""); + static_assert((std::is_nothrow_convertible_v<D, C>), ""); + + static_assert((std::is_nothrow_convertible_v<const void, void>), ""); + static_assert((std::is_nothrow_convertible_v<volatile void, void>), ""); + static_assert((std::is_nothrow_convertible_v<void, const void>), ""); + static_assert((std::is_nothrow_convertible_v<void, volatile void>), ""); + + static_assert(!(std::is_nothrow_convertible_v<int[], double[]>), ""); + static_assert(!(std::is_nothrow_convertible_v<int[], int[]>), ""); + static_assert(!(std::is_nothrow_convertible_v<int[10], int[10]>), ""); + static_assert(!(std::is_nothrow_convertible_v<int[10], double[10]>), ""); + static_assert(!(std::is_nothrow_convertible_v<int[5], double[10]>), ""); + static_assert(!(std::is_nothrow_convertible_v<int[10], A[10]>), ""); + + typedef void V(); + typedef int I(); + static_assert(!(std::is_nothrow_convertible_v<V, V>), ""); + static_assert(!(std::is_nothrow_convertible_v<V, I>), ""); + + return 0; +} diff --git a/libcxx/www/cxx2a_status.html b/libcxx/www/cxx2a_status.html index fc47072ac5f..d6eb3c7cddf 100644 --- a/libcxx/www/cxx2a_status.html +++ b/libcxx/www/cxx2a_status.html @@ -92,7 +92,7 @@ <tr><td><a href="https://wg21.link/P0619R4">P0619R4</a></td><td>LWG</td><td>Reviewing Deprecated Facilities of C++17 for C++20</td><td>Rapperswil</td><td></td><td></td></tr> <tr><td><a href="https://wg21.link/P0646R1">P0646R1</a></td><td>LWG</td><td>Improving the Return Value of Erase-Like Algorithms</td><td>Rapperswil</td><td></td><td></td></tr> <tr><td><a href="https://wg21.link/P0722R3">P0722R3</a></td><td>CWG</td><td>Efficient sized delete for variable sized classes</td><td>Rapperswil</td><td></td><td></td></tr> - <tr><td><a href="https://wg21.link/P0758R1">P0758R1</a></td><td>LWG</td><td>Implicit conversion traits and utility functions</td><td>Rapperswil</td><td></td><td></td></tr> + <tr><td><a href="https://wg21.link/P0758R1">P0758R1</a></td><td>LWG</td><td>Implicit conversion traits and utility functions</td><td>Rapperswil</td><td>Complete</td><td></td></tr> <tr><td><a href="https://wg21.link/P0759R1">P0759R1</a></td><td>LWG</td><td>fpos Requirements</td><td>Rapperswil</td><td></td><td></td></tr> <tr><td><a href="https://wg21.link/P0769R2">P0769R2</a></td><td>LWG</td><td>Add shift to <algorithm></td><td>Rapperswil</td><td></td><td></td></tr> <tr><td><a href="https://wg21.link/P0788R3">P0788R3</a></td><td>LWG</td><td>Standard Library Specification in a Concepts and Contracts World</td><td>Rapperswil</td><td></td><td></td></tr> |

