[cfe-commits] [libcxx] r153855 - in /libcxx/trunk: include/__tuple include/tuple test/utilities/tuple/tuple.tuple/tuple.cnstr/UTypes.fail.cpp test/utilities/tuple/tuple.tuple/tuple.cnstr/const_Types.fail.cpp

Howard Hinnant hhinnant at apple.com
Sun Apr 1 16:10:42 PDT 2012


Author: hhinnant
Date: Sun Apr  1 18:10:42 2012
New Revision: 153855

URL: http://llvm.org/viewvc/llvm-project?rev=153855&view=rev
Log:
I believe tuple is still under development in the standard.  Daniel Krugler is/will be making convincing arguments that a modified form of LWG 2051 (currently NAD Future) is easily acheivable and desirable.  He has demonstrated that a tuple<T...> where all of the T are implicitly convertible from U... should have a tuple constructor that is also implicit, instead of explicit.  This would support the use cases in LWG 2051 while not undermining T... with explicit conversions from U....  This check-in is an experimental implementation of Daniel's work.  I believe this work to be mature enough to warrant inclusion into libc++.  If anyone sees real-world problems that this check in causes, please let me know and I will revert it, and provide the feedback to the LWG.

Modified:
    libcxx/trunk/include/__tuple
    libcxx/trunk/include/tuple
    libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/UTypes.fail.cpp
    libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/const_Types.fail.cpp

Modified: libcxx/trunk/include/__tuple
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/__tuple?rev=153855&r1=153854&r2=153855&view=diff
==============================================================================
--- libcxx/trunk/include/__tuple (original)
+++ libcxx/trunk/include/__tuple Sun Apr  1 18:10:42 2012
@@ -216,7 +216,7 @@
 template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
 struct __tuple_convertible_imp<true, __tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
     : public integral_constant<bool,
-                               is_constructible<_Up0, _Tp0>::value &&
+                               is_convertible<_Tp0, _Up0>::value &&
                                __tuple_convertible_imp<true, __tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
 
 template <>
@@ -235,6 +235,33 @@
              typename __make_tuple_types<_Tp>::type, typename __make_tuple_types<_Up>::type>
 {};
 
+// __tuple_constructible
+
+template <bool, class _Tp, class _Up>
+struct __tuple_constructible_imp : public false_type {};
+
+template <class _Tp0, class ..._Tp, class _Up0, class ..._Up>
+struct __tuple_constructible_imp<true, __tuple_types<_Tp0, _Tp...>, __tuple_types<_Up0, _Up...> >
+    : public integral_constant<bool,
+                               is_constructible<_Up0, _Tp0>::value &&
+                               __tuple_constructible_imp<true, __tuple_types<_Tp...>, __tuple_types<_Up...> >::value> {};
+
+template <>
+struct __tuple_constructible_imp<true, __tuple_types<>, __tuple_types<> >
+    : public true_type {};
+
+template <class _Tp, class _Up, bool = __tuple_like<typename remove_reference<_Tp>::type>::value,
+                                bool = __tuple_like<_Up>::value>
+struct __tuple_constructible
+    : public false_type {};
+
+template <class _Tp, class _Up>
+struct __tuple_constructible<_Tp, _Up, true, true>
+    : public __tuple_constructible_imp<tuple_size<typename remove_reference<_Tp>::type>::value ==
+                                     tuple_size<_Up>::value,
+             typename __make_tuple_types<_Tp>::type, typename __make_tuple_types<_Up>::type>
+{};
+
 // __tuple_assignable
 
 template <bool, class _Tp, class _Up>

Modified: libcxx/trunk/include/tuple
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/tuple?rev=153855&r1=153854&r2=153855&view=diff
==============================================================================
--- libcxx/trunk/include/tuple (original)
+++ libcxx/trunk/include/tuple Sun Apr  1 18:10:42 2012
@@ -552,7 +552,7 @@
                ) {}
 
     template <class ..._Up,
-              class = typename enable_if
+              typename enable_if
                       <
                          sizeof...(_Up) <= sizeof...(_Tp) &&
                          __tuple_convertible
@@ -562,8 +562,40 @@
                                      sizeof...(_Up) < sizeof...(_Tp) ?
                                         sizeof...(_Up) :
                                         sizeof...(_Tp)>::type
-                         >::value
-                      >::type
+                         >::value,
+                         bool
+                      >::type = false
+             >
+        _LIBCPP_INLINE_VISIBILITY
+        tuple(_Up&&... __u)
+            : base_(typename __make_tuple_indices<sizeof...(_Up)>::type(),
+                    typename __make_tuple_types<tuple, sizeof...(_Up)>::type(),
+                    typename __make_tuple_indices<sizeof...(_Tp), sizeof...(_Up)>::type(),
+                    typename __make_tuple_types<tuple, sizeof...(_Tp), sizeof...(_Up)>::type(),
+                    _VSTD::forward<_Up>(__u)...) {}
+
+    template <class ..._Up,
+              typename enable_if
+                      <
+                         sizeof...(_Up) <= sizeof...(_Tp) &&
+                         __tuple_constructible
+                         <
+                            tuple<_Up...>,
+                            typename __make_tuple_types<tuple,
+                                     sizeof...(_Up) < sizeof...(_Tp) ?
+                                        sizeof...(_Up) :
+                                        sizeof...(_Tp)>::type
+                         >::value &&
+                         !__tuple_convertible
+                         <
+                            tuple<_Up...>,
+                            typename __make_tuple_types<tuple,
+                                     sizeof...(_Up) < sizeof...(_Tp) ?
+                                        sizeof...(_Up) :
+                                        sizeof...(_Tp)>::type
+                         >::value,
+                         bool
+                      >::type =false
              >
         _LIBCPP_INLINE_VISIBILITY
         explicit
@@ -598,15 +630,29 @@
                     _VSTD::forward<_Up>(__u)...) {}
 
     template <class _Tuple,
-              class = typename enable_if
+              typename enable_if
                       <
-                         __tuple_convertible<_Tuple, tuple>::value
-                      >::type
+                         __tuple_convertible<_Tuple, tuple>::value,
+                         bool
+                      >::type = false
              >
         _LIBCPP_INLINE_VISIBILITY
         tuple(_Tuple&& __t)
             : base_(_VSTD::forward<_Tuple>(__t)) {}
 
+    template <class _Tuple,
+              typename enable_if
+                      <
+                         __tuple_constructible<_Tuple, tuple>::value &&
+                         !__tuple_convertible<_Tuple, tuple>::value,
+                         bool
+                      >::type = false
+             >
+        _LIBCPP_INLINE_VISIBILITY
+        explicit
+        tuple(_Tuple&& __t)
+            : base_(_VSTD::forward<_Tuple>(__t)) {}
+
     template <class _Alloc, class _Tuple,
               class = typename enable_if
                       <

Modified: libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/UTypes.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/UTypes.fail.cpp?rev=153855&r1=153854&r2=153855&view=diff
==============================================================================
--- libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/UTypes.fail.cpp (original)
+++ libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/UTypes.fail.cpp Sun Apr  1 18:10:42 2012
@@ -14,14 +14,36 @@
 // template <class... UTypes>
 //   explicit tuple(UTypes&&... u);
 
+/*
+    This is testing an extension whereby only Types having an explicit conversion
+    from UTypes are bound by the explicit tuple constructor.
+*/
+
 #include <tuple>
 #include <cassert>
 
-#include "../MoveOnly.h"
+class MoveOnly
+{
+    MoveOnly(const MoveOnly&);
+    MoveOnly& operator=(const MoveOnly&);
+
+    int data_;
+public:
+    explicit MoveOnly(int data = 1) : data_(data) {}
+    MoveOnly(MoveOnly&& x)
+        : data_(x.data_) {x.data_ = 0;}
+    MoveOnly& operator=(MoveOnly&& x)
+        {data_ = x.data_; x.data_ = 0; return *this;}
+
+    int get() const {return data_;}
+
+    bool operator==(const MoveOnly& x) const {return data_ == x.data_;}
+    bool operator< (const MoveOnly& x) const {return data_ <  x.data_;}
+};
 
 int main()
 {
     {
-        std::tuple<MoveOnly> t = MoveOnly(0);
+        std::tuple<MoveOnly> t = 1;
     }
 }

Modified: libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/const_Types.fail.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/const_Types.fail.cpp?rev=153855&r1=153854&r2=153855&view=diff
==============================================================================
--- libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/const_Types.fail.cpp (original)
+++ libcxx/trunk/test/utilities/tuple/tuple.tuple/tuple.cnstr/const_Types.fail.cpp Sun Apr  1 18:10:42 2012
@@ -20,7 +20,6 @@
 int main()
 {
     {
-        std::tuple<int> t = 2;
-        assert(std::get<0>(t) == 2);
+        std::tuple<int*> t = 0;
     }
 }





More information about the cfe-commits mailing list