[libcxx] r229696 - [libc++] Fix PR20084 - std::is_function<void() const> failed.
Eric Fiselier
eric at efcs.ca
Wed Feb 18 08:31:46 PST 2015
Author: ericwf
Date: Wed Feb 18 10:31:46 2015
New Revision: 229696
URL: http://llvm.org/viewvc/llvm-project?rev=229696&view=rev
Log:
[libc++] Fix PR20084 - std::is_function<void() const> failed.
Summary:
This patch introduces some black magic to detect const and volatile qualified function types such as `void () const`.
The patch works in the following way:
We first rule out any type that satisfies on of the following. These restrictions are important so that the test below works properly.
* `is_class<_Tp>::value`
* `is_union<_Tp>::value`
* `is_void<_Tp>::value`
* `is_reference<_Tp>::value`
* `__is_nullptr_t<_Tp>::value`
If none of the above is true we perform overload resolution on `__source<_Tp>(0)` to determine the return type.
* If `_Tp&` is well-formed we select `_Tp& __source(int)`. `_Tp&` is only ill formed for cv void types and cv/ref qualified function types.
* Otherwise we select `__dummy_type __source(...)`. Since we know `_Tp` cannot be void then it must be a function type.
let `R` be the returned from `__source<_Tp>(0)`.
We perform overload resolution on `__test<_Tp>(R)`.
* If `R` is `__dummy_type` we call `true_type __test(__dummy_type)`.
* if `R` is `_Tp&` and `_Tp&` decays to `_Tp*` we call `true_type __test(_Tp*)`. Only references to function types decay to a pointer of the same type.
* In all other cases we call `false_type __test(...)`.
`__source<_Tp>(0)` will try and form `_Tp&` in the return type. if `_Tp&` is not well formed the return type of `__source<_Tp>(0)` will be dummy type. `_Tp&` is only ill-formed for cv/ref qualified function types (and void which is dealt with elsewhere).
This fixes PR20084 - http://llvm.org/bugs/show_bug.cgi?id=20084
Reviewers: rsmith, K-ballo, mclow.lists
Reviewed By: mclow.lists
Subscribers: cfe-commits
Differential Revision: http://reviews.llvm.org/D7573
Modified:
libcxx/trunk/include/type_traits
libcxx/trunk/test/std/utilities/meta/meta.unary/meta.unary.cat/function.pass.cpp
Modified: libcxx/trunk/include/type_traits
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=229696&r1=229695&r2=229696&view=diff
==============================================================================
--- libcxx/trunk/include/type_traits (original)
+++ libcxx/trunk/include/type_traits Wed Feb 18 10:31:46 2015
@@ -430,9 +430,12 @@ template <class _Tp> struct _
namespace __libcpp_is_function_imp
{
+struct __dummy_type {};
template <class _Tp> char __test(_Tp*);
+template <class _Tp> char __test(__dummy_type);
template <class _Tp> __two __test(...);
-template <class _Tp> _Tp& __source();
+template <class _Tp> _Tp& __source(int);
+template <class _Tp> __dummy_type __source(...);
}
template <class _Tp, bool = is_class<_Tp>::value ||
@@ -441,7 +444,7 @@ template <class _Tp, bool = is_class<_Tp
is_reference<_Tp>::value ||
__is_nullptr_t<_Tp>::value >
struct __libcpp_is_function
- : public integral_constant<bool, sizeof(__libcpp_is_function_imp::__test<_Tp>(__libcpp_is_function_imp::__source<_Tp>())) == 1>
+ : public integral_constant<bool, sizeof(__libcpp_is_function_imp::__test<_Tp>(__libcpp_is_function_imp::__source<_Tp>(0))) == 1>
{};
template <class _Tp> struct __libcpp_is_function<_Tp, true> : public false_type {};
Modified: libcxx/trunk/test/std/utilities/meta/meta.unary/meta.unary.cat/function.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/meta/meta.unary/meta.unary.cat/function.pass.cpp?rev=229696&r1=229695&r2=229696&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/meta/meta.unary/meta.unary.cat/function.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/meta/meta.unary/meta.unary.cat/function.pass.cpp Wed Feb 18 10:31:46 2015
@@ -13,8 +13,19 @@
#include <type_traits>
+using namespace std;
+
+class Class {};
+
+enum Enum1 {};
+#if __cplusplus >= 201103L
+enum class Enum2 : int {};
+#else
+enum Enum2 {};
+#endif
+
template <class T>
-void test_function_imp()
+void test()
{
static_assert(!std::is_void<T>::value, "");
#if _LIBCPP_STD_VER > 11
@@ -34,19 +45,44 @@ void test_function_imp()
static_assert( std::is_function<T>::value, "");
}
-template <class T>
-void test_function()
-{
- test_function_imp<T>();
- test_function_imp<const T>();
- test_function_imp<volatile T>();
- test_function_imp<const volatile T>();
-}
+// Since we can't actually add the const volatile and ref qualifiers once
+// later let's use a macro to do it.
+#define TEST_REGULAR(...) \
+ test<__VA_ARGS__>(); \
+ test<__VA_ARGS__ const>(); \
+ test<__VA_ARGS__ volatile>(); \
+ test<__VA_ARGS__ const volatile>()
+
+
+#define TEST_REF_QUALIFIED(...) \
+ test<__VA_ARGS__ &>(); \
+ test<__VA_ARGS__ const &>(); \
+ test<__VA_ARGS__ volatile &>(); \
+ test<__VA_ARGS__ const volatile &>(); \
+ test<__VA_ARGS__ &&>(); \
+ test<__VA_ARGS__ const &&>(); \
+ test<__VA_ARGS__ volatile &&>(); \
+ test<__VA_ARGS__ const volatile &&>()
+
int main()
{
- test_function<void ()>();
- test_function<void (int)>();
- test_function<int (double)>();
- test_function<int (double, char)>();
+ TEST_REGULAR( void () );
+ TEST_REGULAR( void (int) );
+ TEST_REGULAR( int (double) );
+ TEST_REGULAR( int (double, char) );
+ TEST_REGULAR( void (...) );
+ TEST_REGULAR( void (int, ...) );
+ TEST_REGULAR( int (double, ...) );
+ TEST_REGULAR( int (double, char, ...) );
+#if __cplusplus >= 201103L
+ TEST_REF_QUALIFIED( void () );
+ TEST_REF_QUALIFIED( void (int) );
+ TEST_REF_QUALIFIED( int (double) );
+ TEST_REF_QUALIFIED( int (double, char) );
+ TEST_REF_QUALIFIED( void (...) );
+ TEST_REF_QUALIFIED( void (int, ...) );
+ TEST_REF_QUALIFIED( int (double, ...) );
+ TEST_REF_QUALIFIED( int (double, char, ...) );
+#endif
}
More information about the cfe-commits
mailing list