[libcxx] r230120 - [libc++] Try and prevent evaluation of `is_default_constructible` on tuples default constructor if it is not needed.
Eric Fiselier
eric at efcs.ca
Fri Feb 20 18:30:41 PST 2015
Author: ericwf
Date: Fri Feb 20 20:30:41 2015
New Revision: 230120
URL: http://llvm.org/viewvc/llvm-project?rev=230120&view=rev
Log:
[libc++] Try and prevent evaluation of `is_default_constructible` on tuples default constructor if it is not needed.
Summary:
Currently parts of the SFINAE on tuples default constructor always gets evaluated even when the default constructor is never called or instantiated. This can cause a hard compile error when a tuple is created with types that do not have a default constructor. Below is a self contained example using a pair like class. This code will not compile but probably should.
```
#include <type_traits>
template <class T>
struct IllFormedDefaultImp {
IllFormedDefaultImp(T x) : value(x) {}
constexpr IllFormedDefaultImp() {}
T value;
};
typedef IllFormedDefaultImp<int &> IllFormedDefault;
template <class T, class U>
struct pair
{
template <bool Dummy = true,
class = typename std::enable_if<
std::is_default_constructible<T>::value
&& std::is_default_constructible<U>::value
&& Dummy>::type
>
constexpr pair() : first(), second() {}
pair(T const & t, U const & u) : first(t), second(u) {}
T first;
U second;
};
int main()
{
int x = 1;
IllFormedDefault v(x);
pair<IllFormedDefault, IllFormedDefault> p(v, v);
}
```
One way to fix this is to use `Dummy` in a more involved way in the constructor SFINAE. The following patch fixes these sorts of hard compile errors for tuple.
Reviewers: mclow.lists, rsmith, K-ballo, EricWF
Reviewed By: EricWF
Subscribers: ldionne, cfe-commits
Differential Revision: http://reviews.llvm.org/D7569
Modified:
libcxx/trunk/include/tuple
libcxx/trunk/include/type_traits
libcxx/trunk/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp
Modified: libcxx/trunk/include/tuple
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/tuple?rev=230120&r1=230119&r2=230120&view=diff
==============================================================================
--- libcxx/trunk/include/tuple (original)
+++ libcxx/trunk/include/tuple Fri Feb 20 20:30:41 2015
@@ -511,8 +511,8 @@ class _LIBCPP_TYPE_VIS_ONLY tuple
typename tuple_element<_Jp, tuple<_Up...> >::type&& get(tuple<_Up...>&&) _NOEXCEPT;
public:
- template <bool _Dummy = true, class _Up = typename enable_if<
- __all<(_Dummy && is_default_constructible<_Tp>::value)...>::value
+ template <bool _Dummy = true, class = typename enable_if<
+ __all<__dependent_type<is_default_constructible<_Tp>, _Dummy>::value...>::value
>::type>
_LIBCPP_INLINE_VISIBILITY
_LIBCPP_CONSTEXPR tuple()
Modified: libcxx/trunk/include/type_traits
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/type_traits?rev=230120&r1=230119&r2=230120&view=diff
==============================================================================
--- libcxx/trunk/include/type_traits (original)
+++ libcxx/trunk/include/type_traits Fri Feb 20 20:30:41 2015
@@ -216,6 +216,9 @@ template <class...>
struct __void_t { typedef void type; };
#endif
+template <class _Tp, bool>
+struct _LIBCPP_TYPE_VIS_ONLY __dependent_type : public _Tp {};
+
template <bool _Bp, class _If, class _Then>
struct _LIBCPP_TYPE_VIS_ONLY conditional {typedef _If type;};
template <class _If, class _Then>
Modified: libcxx/trunk/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp?rev=230120&r1=230119&r2=230120&view=diff
==============================================================================
--- libcxx/trunk/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp (original)
+++ libcxx/trunk/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp Fri Feb 20 20:30:41 2015
@@ -35,6 +35,16 @@ struct ThrowingDefault {
ThrowingDefault() { }
};
+struct IllFormedDefault {
+ IllFormedDefault(int x) : value(x) {}
+ template <bool Pred = false>
+ constexpr IllFormedDefault() {
+ static_assert(Pred,
+ "The default constructor should not be instantiated");
+ }
+ int value;
+};
+
int main()
{
{
@@ -89,5 +99,12 @@ int main()
assert(std::get<0>(t) == 0);
assert(std::get<1>(t) == nullptr);
}
+ {
+ // Check that the SFINAE on the default constructor is not evaluted when
+ // it isn't needed. If the default constructor is evaluted then this test
+ // should fail to compile.
+ IllFormedDefault v(0);
+ std::tuple<IllFormedDefault> t(v);
+ }
#endif
}
More information about the cfe-commits
mailing list