[libcxx-commits] [PATCH] D58987: Make underlying_type SFINAE-friendly

Zoe Carver via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed Mar 6 13:49:37 PST 2019


zoecarver updated this revision to Diff 189583.
zoecarver marked an inline comment as done.
zoecarver added a comment.

Add fail test, update pass test to work with any version, use `__impl_...`.


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

https://reviews.llvm.org/D58987

Files:
  include/type_traits
  test/std/utilities/meta/meta.trans/meta.trans.other/underlying_type.fail.cpp
  test/std/utilities/meta/meta.trans/meta.trans.other/underlying_type.pass.cpp


Index: test/std/utilities/meta/meta.trans/meta.trans.other/underlying_type.pass.cpp
===================================================================
--- test/std/utilities/meta/meta.trans/meta.trans.other/underlying_type.pass.cpp
+++ test/std/utilities/meta/meta.trans/meta.trans.other/underlying_type.pass.cpp
@@ -27,6 +27,13 @@
 enum F { W = UINT_MAX };
 #endif // TEST_UNSIGNED_UNDERLYING_TYPE
 
+template<class T>
+std::enable_if_t<
+	std::is_same_v<std::underlying_type_t<T>, int>
+> test_sfinae(T t);
+
+void test_sfinae(int) { return; }
+
 int main(int, char**)
 {
     static_assert((std::is_same<std::underlying_type<E>::type, int>::value),
@@ -52,6 +59,8 @@
     static_assert((std::is_same<std::underlying_type_t<G>, char>::value), "");
 #endif // TEST_STD_VER > 11
 #endif // TEST_STD_VER >= 11
+	
+	test_sfinae(0);
 
   return 0;
 }
Index: test/std/utilities/meta/meta.trans/meta.trans.other/underlying_type.fail.cpp
===================================================================
--- /dev/null
+++ test/std/utilities/meta/meta.trans/meta.trans.other/underlying_type.fail.cpp
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+
+// underlying_type
+
+#include <type_traits>
+
+template<class T>
+std::enable_if_t<
+	std::is_same_v<std::underlying_type_t<T>, int>
+> test_sfinae(T t);
+
+int main(int, char**)
+{
+	test_sfinae(0); // expected-error {{no matching function for call to 'test_sfinae'}}
+
+  return 0;
+}
Index: include/type_traits
===================================================================
--- include/type_traits
+++ include/type_traits
@@ -4711,12 +4711,18 @@
 
 #ifdef _LIBCPP_UNDERLYING_TYPE
 
-template <class _Tp>
-struct underlying_type
+template <class _Tp, bool = is_enum<_Tp>::value>
+struct __impl_underlying_type
 {
     typedef _LIBCPP_UNDERLYING_TYPE(_Tp) type;
 };
 
+template <class _Tp>
+struct __impl_underlying_type<_Tp, false> { };
+
+template <class _Tp>
+struct underlying_type: __impl_underlying_type<_Tp> { };
+
 #if _LIBCPP_STD_VER > 11
 template <class _Tp> using underlying_type_t = typename underlying_type<_Tp>::type;
 #endif


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58987.189583.patch
Type: text/x-patch
Size: 2458 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20190306/0207841c/attachment-0001.bin>


More information about the libcxx-commits mailing list