[libc++] Add is_nothrow_convertible from P0758R1
authorLouis Dionne <ldionne@apple.com>
Wed, 27 Feb 2019 17:57:38 +0000 (17:57 +0000)
committerLouis Dionne <ldionne@apple.com>
Wed, 27 Feb 2019 17:57:38 +0000 (17:57 +0000)
Reviewed as https://reviews.llvm.org/D58019.
Thanks to Zoe Carver for the patch.

llvm-svn: 355010

libcxx/include/type_traits
libcxx/test/std/type_traits/is_nothrow_convertible.pass.cpp [new file with mode: 0644]
libcxx/www/cxx2a_status.html

index 965ae7a29e6d836010416753df3430c6da2727de..cd67eb89e0384701220aee9b7978d3b348410cd5 100644 (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)
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 (file)
index 0000000..2a953d9
--- /dev/null
@@ -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;
+}
index fc47072ac5f1cd9be4123e4273b8e8d4fd8b7ded..d6eb3c7cddf226fbff16c4a1b76dd9685f5af3a0 100644 (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>