[libcxx] Fix tuple construction/assignment from types derived from tuple/pair/array.

Summary:
The standard requires tuple have the following constructors:
```
tuple(tuple<OtherTypes...> const&);
tuple(tuple<OtherTypes...> &&);
tuple(pair<T1, T2> const&);
tuple(pair<T1, T2> &&);
tuple(array<T, N> const&);
tuple(array<T, N> &&);
```
However libc++ implements these as a single constructor with the signature:
```
template <class TupleLike, enable_if_t<__is_tuple_like<TupleLike>::value>>
tuple(TupleLike&&);
```

This causes the constructor to reject types derived from tuple-like types; Unlike if we had all of the concrete overloads, because they cause the derived->base conversion in the signature.

This patch fixes this issue by detecting derived types and the tuple-like base they are derived from. It does this by creating an overloaded function with signatures for each of tuple/pair/array and checking if the possibly derived type can convert to any of them.

This patch fixes [PR17550]( https://llvm.org/bugs/show_bug.cgi?id=17550)

This patch 

Reviewers: mclow.lists, K-ballo, mpark, EricWF

Subscribers: cfe-commits

Differential Revision: https://reviews.llvm.org/D27606

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@289727 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Eric Fiselier
2016-12-14 22:22:38 +00:00
parent a793c18bae
commit 18e56b438e
6 changed files with 556 additions and 34 deletions

View File

@@ -453,19 +453,57 @@ using tuple_element_t = typename tuple_element <_Ip, _Tp...>::type;
#endif // _LIBCPP_HAS_NO_VARIADICS
#ifndef _LIBCPP_CXX03_LANG
template <bool _IsTuple, class _SizeTrait, size_t _Expected>
struct __tuple_like_with_size_imp : false_type {};
template <class _SizeTrait, size_t _Expected>
struct __tuple_like_with_size_imp<true, _SizeTrait, _Expected>
: integral_constant<bool, _SizeTrait::value == _Expected> {};
template <class _Tp, size_t _TSize, bool _Good = true>
struct __lookup_result {
using type = _Tp;
static constexpr bool _Success = _Good;
static constexpr size_t _Size = _TSize;
};
using __lookup_failure = __lookup_result<void, (size_t)-1, false>;
template <class _Tuple, size_t _ExpectedSize,
class _RawTuple = typename __uncvref<_Tuple>::type>
using __tuple_like_with_size = __tuple_like_with_size_imp<
__tuple_like<_RawTuple>::value,
tuple_size<_RawTuple>, _ExpectedSize
>;
template <class ..._Args>
auto __deduce_tuple_type_ovl(tuple<_Args...>&)
-> __lookup_result<tuple<_Args...>, sizeof...(_Args)>;
template <class _T1, class _T2>
auto __deduce_tuple_type_ovl(pair<_T1, _T2>&)
-> __lookup_result<pair<_T1, _T2>, 2>;
template <class _Tp, size_t _Size>
auto __deduce_tuple_type_ovl(array<_Tp, _Size>&)
-> __lookup_result<array<_Tp, _Size>, _Size>;
template <class _Tp>
auto __deduce_tuple_type_imp(int)
-> decltype(__deduce_tuple_type_ovl(_VSTD::declval<__uncvref_t<_Tp>&>()));
template <class> __lookup_failure __deduce_tuple_type_imp(...);
// __deduce_tuple_like - Given a type determine if it is, or is derived from,
// a tuple-like type. This trait is used to support constructing and assigning
// to std::tuple from user-types derived from a tuple-like type.
template <class _TupleLike,
class _Result = decltype(__deduce_tuple_type_imp<_TupleLike>(0)),
bool _Good = _Result::_Success>
struct __deduce_tuple_like {
static_assert(_Good, "incorrect specialization choosen");
static constexpr bool _Success = true;
static constexpr size_t _Size = _Result::_Size;
using _RawType = typename _Result::type;
using _QualType =
typename __propagate_value_category<_TupleLike>::template __apply<_RawType>;
};
template <class _TupleLike, class _Result>
struct __deduce_tuple_like<_TupleLike, _Result, /*_Good=*/false> {
static constexpr bool _Success = false;
static constexpr size_t _Size = (size_t)-1;
};
template <class _TupleLike, size_t _ExpectedSize,
class _Deduced = __deduce_tuple_like<_TupleLike>>
using __tuple_like_with_size = integral_constant<bool,
_Deduced::_Success && _Deduced::_Size == _ExpectedSize>;
struct _LIBCPP_TYPE_VIS __check_tuple_constructor_fail {
template <class ...>