[PATCH] [libc++] Try and prevent evaluation of `is_default_constructible` on tuples default constructor if it is not needed.

Eric Fiselier eric at efcs.ca
Wed Feb 11 15:07:25 PST 2015


Hi mclow.lists, rsmith, K-ballo,

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.

http://reviews.llvm.org/D7569

Files:
  include/tuple
  include/type_traits
  test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp

Index: include/tuple
===================================================================
--- include/tuple
+++ include/tuple
@@ -512,7 +512,7 @@
 public:
 
     template <bool _Dummy = true, class _Up = typename enable_if<
-        __all<(_Dummy && is_default_constructible<_Tp>::value)...>::value
+        __all<__dependent_is_default_constructible<_Tp, _Dummy>::value...>::value
     >::type>
     _LIBCPP_INLINE_VISIBILITY
     _LIBCPP_CONSTEXPR tuple()
Index: include/type_traits
===================================================================
--- include/type_traits
+++ include/type_traits
@@ -2646,6 +2646,11 @@
     : public is_constructible<_Tp>
     {};
 
+template <class _Tp, bool>
+struct _LIBCPP_TYPE_VIS_ONLY __dependent_is_default_constructible
+    : public is_default_constructible<_Tp>
+    {};
+
 // is_copy_constructible
 
 template <class _Tp>
Index: test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp
===================================================================
--- test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp
+++ test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp
@@ -33,6 +33,15 @@
     ThrowingDefault() { }
 };
 
+template <class T>
+struct IllFormedDefaultImp {
+    IllFormedDefaultImp(T x) : value(x) {}
+    constexpr IllFormedDefaultImp() {}
+    T value;
+};
+
+typedef IllFormedDefaultImp<int &> IllFormedDefault;
+
 int main()
 {
     {
@@ -87,5 +96,13 @@
         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.
+        int x = 0;
+        IllFormedDefault v(x);
+        std::tuple<IllFormedDefault> t(v);
+    }
 #endif
 }

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7569.19785.patch
Type: text/x-patch
Size: 1853 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150211/b80fa834/attachment.bin>


More information about the cfe-commits mailing list