[libcxx-commits] [PATCH] D58019: Add is_nothrow_convertible (P0758R1)

Zoe Carver via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Feb 13 12:39:44 PST 2019


zoecarver updated this revision to Diff 186720.
zoecarver marked 3 inline comments as done.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D58019/new/

https://reviews.llvm.org/D58019

Files:
  include/type_traits
  test/std/type_traits/is_nothrow_convertible.pass.cpp


Index: test/std/type_traits/is_nothrow_convertible.pass.cpp
===================================================================
--- /dev/null
+++ 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;
+}
Index: include/type_traits
===================================================================
--- include/type_traits
+++ include/type_traits
@@ -1549,6 +1549,32 @@
     = 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)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58019.186720.patch
Type: text/x-patch
Size: 3510 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190213/63478ff1/attachment.bin>


More information about the libcxx-commits mailing list