From 65500d4b29bdd03f6e3d14bd09550465bd9e16ed Mon Sep 17 00:00:00 2001 From: Eric Fiselier Date: Sat, 21 Feb 2015 02:30:41 +0000 Subject: [PATCH] [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 template struct IllFormedDefaultImp { IllFormedDefaultImp(T x) : value(x) {} constexpr IllFormedDefaultImp() {} T value; }; typedef IllFormedDefaultImp IllFormedDefault; template struct pair { template ::value && std::is_default_constructible::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 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 llvm-svn: 230120 --- libcxx/include/tuple | 4 ++-- libcxx/include/type_traits | 3 +++ .../tuple/tuple.tuple/tuple.cnstr/default.pass.cpp | 17 +++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/libcxx/include/tuple b/libcxx/include/tuple index 93518d8..21fa900 100644 --- a/libcxx/include/tuple +++ b/libcxx/include/tuple @@ -511,8 +511,8 @@ class _LIBCPP_TYPE_VIS_ONLY tuple typename tuple_element<_Jp, tuple<_Up...> >::type&& get(tuple<_Up...>&&) _NOEXCEPT; public: - template ::value)...>::value + template , _Dummy>::value...>::value >::type> _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR tuple() diff --git a/libcxx/include/type_traits b/libcxx/include/type_traits index 52fb590..1820bb2 100644 --- a/libcxx/include/type_traits +++ b/libcxx/include/type_traits @@ -216,6 +216,9 @@ template struct __void_t { typedef void type; }; #endif +template +struct _LIBCPP_TYPE_VIS_ONLY __dependent_type : public _Tp {}; + template struct _LIBCPP_TYPE_VIS_ONLY conditional {typedef _If type;}; template diff --git a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp index 8578d7f..d282c9c 100644 --- a/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp +++ b/libcxx/test/std/utilities/tuple/tuple.tuple/tuple.cnstr/default.pass.cpp @@ -35,6 +35,16 @@ struct ThrowingDefault { ThrowingDefault() { } }; +struct IllFormedDefault { + IllFormedDefault(int x) : value(x) {} + template + 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 t(v); + } #endif } -- 2.7.4