[libcxx] r178576 - Richard Smith: It was pointed out to me off-list that libc++'s non-compiler-builtin

Howard Hinnant hhinnant at apple.com
Tue Apr 2 14:25:06 PDT 2013


Author: hhinnant
Date: Tue Apr  2 16:25:06 2013
New Revision: 178576

URL: http://llvm.org/viewvc/llvm-project?rev=178576&view=rev
Log:
Richard Smith: It was pointed out to me off-list that libc++'s non-compiler-builtin
implementation of std::is_polymorphic does this:

template <class _Tp> struct __is_polymorphic1 : public _Tp {};

... and that g++ rejects this if _Tp has an inaccessible virtual destructor
(because __is_polymorphic1<_Tp> would have a deleted virtual destructor
overriding _Tp's non-deleted destructor). Clang was failing to reject this;
I've fixed that in r178563, but that causes libc++'s corresponding test
case to fail with both clang and gcc when using the fallback
implementation. The fallback code also incorrectly rejects final types.

The attached patch fixes the fallback implementation of is_polymorphic; we
now use dynamic_cast's detection of polymorphic class types rather than
trying to determine if adding a virtual function makes the type larger:

  enable_if<sizeof((_Tp*)dynamic_cast<const volatile
void*>(declval<_Tp*>())) != 0, ...>

Two things of note here:
* the (_Tp*) cast is necessary to work around bugs in Clang and g++ where
we otherwise don't instantiate the dynamic_cast (filed as PR15656)
* the 'const volatile' is here to treat is_polymorphic<cv T> as true for a
polymorphic class type T -- my reading of the standard suggests this is
incorrect, but it matches our builtin __is_polymorphic and gcc

Modified:
    libcxx/trunk/include/type_traits
    libcxx/trunk/test/utilities/meta/meta.unary/meta.unary.prop/is_polymorphic.pass.cpp

Modified: libcxx/trunk/include/type_traits
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=178576&r1=178575&r2=178576&view=diff
==============================================================================
--- libcxx/trunk/include/type_traits (original)
+++ libcxx/trunk/include/type_traits Tue Apr  2 16:25:06 2013
@@ -802,17 +802,13 @@ struct _LIBCPP_TYPE_VIS is_polymorphic
 
 #else
 
-template <class _Tp> struct __is_polymorphic1 : public _Tp {};
-template <class _Tp> struct __is_polymorphic2 : public _Tp {virtual ~__is_polymorphic2() throw();};
-
-template <class _Tp, bool = is_class<_Tp>::value>
-struct __libcpp_polymorphic
-    : public integral_constant<bool, sizeof(__is_polymorphic1<_Tp>) == sizeof(__is_polymorphic2<_Tp>)> {};
-
-template <class _Tp> struct __libcpp_polymorphic<_Tp, false> : public false_type {};
+template<typename _Tp> char &__is_polymorphic_impl(
+    typename enable_if<sizeof((_Tp*)dynamic_cast<const volatile void*>(declval<_Tp*>())) != 0,
+                       int>::type);
+template<typename _Tp> __two &__is_polymorphic_impl(...);
 
 template <class _Tp> struct _LIBCPP_TYPE_VIS is_polymorphic
-    : public __libcpp_polymorphic<_Tp> {};
+    : public integral_constant<bool, sizeof(__is_polymorphic_impl<_Tp>(0)) == 1> {};
 
 #endif // __has_feature(is_polymorphic)
 

Modified: libcxx/trunk/test/utilities/meta/meta.unary/meta.unary.prop/is_polymorphic.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/meta/meta.unary/meta.unary.prop/is_polymorphic.pass.cpp?rev=178576&r1=178575&r2=178576&view=diff
==============================================================================
--- libcxx/trunk/test/utilities/meta/meta.unary/meta.unary.prop/is_polymorphic.pass.cpp (original)
+++ libcxx/trunk/test/utilities/meta/meta.unary/meta.unary.prop/is_polymorphic.pass.cpp Tue Apr  2 16:25:06 2013
@@ -52,6 +52,9 @@ class Abstract
     virtual ~Abstract() = 0;
 };
 
+class Final final {
+};
+
 int main()
 {
     test_is_not_polymorphic<void>();
@@ -65,6 +68,9 @@ int main()
     test_is_not_polymorphic<Union>();
     test_is_not_polymorphic<Empty>();
     test_is_not_polymorphic<bit_zero>();
+    test_is_not_polymorphic<Final>();
+    test_is_not_polymorphic<NotEmpty&>();
+    test_is_not_polymorphic<Abstract&>();
 
     test_is_polymorphic<NotEmpty>();
     test_is_polymorphic<Abstract>();





More information about the cfe-commits mailing list