[libc++] Add is_nothrow_convertible from P0758R1

Reviewed as https://reviews.llvm.org/D58019.
Thanks to Zoe Carver for the patch.

llvm-svn: 355010
This commit is contained in:
Louis Dionne 2019-02-27 17:57:38 +00:00
parent 2dca65388c
commit ab19068b7f
3 changed files with 88 additions and 1 deletions

View File

@ -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)

View File

@ -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;
}

View File

@ -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 &lt;algorithm&gt;</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>