[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:
@@ -453,19 +453,57 @@ using tuple_element_t = typename tuple_element <_Ip, _Tp...>::type;
|
|||||||
#endif // _LIBCPP_HAS_NO_VARIADICS
|
#endif // _LIBCPP_HAS_NO_VARIADICS
|
||||||
|
|
||||||
#ifndef _LIBCPP_CXX03_LANG
|
#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>
|
template <class _Tp, size_t _TSize, bool _Good = true>
|
||||||
struct __tuple_like_with_size_imp<true, _SizeTrait, _Expected>
|
struct __lookup_result {
|
||||||
: integral_constant<bool, _SizeTrait::value == _Expected> {};
|
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,
|
template <class ..._Args>
|
||||||
class _RawTuple = typename __uncvref<_Tuple>::type>
|
auto __deduce_tuple_type_ovl(tuple<_Args...>&)
|
||||||
using __tuple_like_with_size = __tuple_like_with_size_imp<
|
-> __lookup_result<tuple<_Args...>, sizeof...(_Args)>;
|
||||||
__tuple_like<_RawTuple>::value,
|
|
||||||
tuple_size<_RawTuple>, _ExpectedSize
|
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 {
|
struct _LIBCPP_TYPE_VIS __check_tuple_constructor_fail {
|
||||||
template <class ...>
|
template <class ...>
|
||||||
|
|||||||
@@ -555,13 +555,19 @@ class _LIBCPP_TYPE_VIS_ONLY tuple
|
|||||||
{
|
{
|
||||||
template <class _Tuple>
|
template <class _Tuple>
|
||||||
static constexpr bool __enable_implicit() {
|
static constexpr bool __enable_implicit() {
|
||||||
return __tuple_convertible<_Tuple, tuple>::value;
|
using _Deduced = __deduce_tuple_like<_Tuple>;
|
||||||
|
using _QualType = typename _Deduced::_QualType;
|
||||||
|
static_assert(__tuple_like<typename _Deduced::_RawType>::value, "");
|
||||||
|
return __tuple_convertible<_QualType, tuple>::value;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tuple>
|
template <class _Tuple>
|
||||||
static constexpr bool __enable_explicit() {
|
static constexpr bool __enable_explicit() {
|
||||||
return __tuple_constructible<_Tuple, tuple>::value
|
using _Deduced = __deduce_tuple_like<_Tuple>;
|
||||||
&& !__tuple_convertible<_Tuple, tuple>::value;
|
using _QualType = typename _Deduced::_QualType;
|
||||||
|
static_assert(__tuple_like<typename _Deduced::_RawType>::value, "");
|
||||||
|
return __tuple_constructible<_QualType, tuple>::value
|
||||||
|
&& !__tuple_convertible<_QualType, tuple>::value;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -814,66 +820,74 @@ public:
|
|||||||
_VSTD::forward<_Up>(__u)...) {}
|
_VSTD::forward<_Up>(__u)...) {}
|
||||||
|
|
||||||
template <class _Tuple,
|
template <class _Tuple,
|
||||||
|
class _Deduced = __deduce_tuple_like<_Tuple>,
|
||||||
|
class _TupBase = typename _Deduced::_QualType,
|
||||||
typename enable_if
|
typename enable_if
|
||||||
<
|
<
|
||||||
_CheckTupleLikeConstructor<
|
_CheckTupleLikeConstructor<
|
||||||
__tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value
|
_Deduced::_Size == sizeof...(_Tp)
|
||||||
&& !_PackExpandsToThisTuple<_Tuple>::value
|
&& !_PackExpandsToThisTuple<_TupBase>::value
|
||||||
>::template __enable_implicit<_Tuple>(),
|
>::template __enable_implicit<_TupBase>(),
|
||||||
bool
|
bool
|
||||||
>::type = false
|
>::type = false
|
||||||
>
|
>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible<base, _Tuple>::value))
|
tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible<base, _TupBase>::value))
|
||||||
: base_(_VSTD::forward<_Tuple>(__t)) {}
|
: base_(_VSTD::forward<_TupBase>(__t)) {}
|
||||||
|
|
||||||
template <class _Tuple,
|
template <class _Tuple,
|
||||||
|
class _Deduced = __deduce_tuple_like<_Tuple>,
|
||||||
|
class _TupBase = typename _Deduced::_QualType,
|
||||||
typename enable_if
|
typename enable_if
|
||||||
<
|
<
|
||||||
_CheckTupleLikeConstructor<
|
_CheckTupleLikeConstructor<
|
||||||
__tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value
|
_Deduced::_Size == sizeof...(_Tp)
|
||||||
&& !_PackExpandsToThisTuple<_Tuple>::value
|
&& !_PackExpandsToThisTuple<_TupBase>::value
|
||||||
>::template __enable_explicit<_Tuple>(),
|
>::template __enable_explicit<_TupBase>(),
|
||||||
bool
|
bool
|
||||||
>::type = false
|
>::type = false
|
||||||
>
|
>
|
||||||
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
_LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR_AFTER_CXX11
|
||||||
explicit
|
explicit
|
||||||
tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible<base, _Tuple>::value))
|
tuple(_Tuple&& __t) _NOEXCEPT_((is_nothrow_constructible<base, _TupBase>::value))
|
||||||
: base_(_VSTD::forward<_Tuple>(__t)) {}
|
: base_(_VSTD::forward<_TupBase>(__t)) {}
|
||||||
|
|
||||||
template <class _Alloc, class _Tuple,
|
template <class _Alloc, class _Tuple,
|
||||||
|
class _Deduced = __deduce_tuple_like<_Tuple>,
|
||||||
|
class _TupBase = typename _Deduced::_QualType,
|
||||||
typename enable_if
|
typename enable_if
|
||||||
<
|
<
|
||||||
_CheckTupleLikeConstructor<
|
_CheckTupleLikeConstructor<
|
||||||
__tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value
|
_Deduced::_Size == sizeof...(_Tp)
|
||||||
>::template __enable_implicit<_Tuple>(),
|
>::template __enable_implicit<_TupBase>(),
|
||||||
bool
|
bool
|
||||||
>::type = false
|
>::type = false
|
||||||
>
|
>
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
|
tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
|
||||||
: base_(allocator_arg_t(), __a, _VSTD::forward<_Tuple>(__t)) {}
|
: base_(allocator_arg_t(), __a, _VSTD::forward<_TupBase>(__t)) {}
|
||||||
|
|
||||||
template <class _Alloc, class _Tuple,
|
template <class _Alloc, class _Tuple,
|
||||||
|
class _Deduced = __deduce_tuple_like<_Tuple>,
|
||||||
|
class _TupBase = typename _Deduced::_QualType,
|
||||||
typename enable_if
|
typename enable_if
|
||||||
<
|
<
|
||||||
_CheckTupleLikeConstructor<
|
_CheckTupleLikeConstructor<
|
||||||
__tuple_like_with_size<_Tuple, sizeof...(_Tp)>::value
|
_Deduced::_Size == sizeof...(_Tp)
|
||||||
>::template __enable_explicit<_Tuple>(),
|
>::template __enable_explicit<_TupBase>(),
|
||||||
bool
|
bool
|
||||||
>::type = false
|
>::type = false
|
||||||
>
|
>
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
explicit
|
explicit
|
||||||
tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
|
tuple(allocator_arg_t, const _Alloc& __a, _Tuple&& __t)
|
||||||
: base_(allocator_arg_t(), __a, _VSTD::forward<_Tuple>(__t)) {}
|
: base_(allocator_arg_t(), __a, _VSTD::forward<_TupBase>(__t)) {}
|
||||||
|
|
||||||
using _CanCopyAssign = __all<is_copy_assignable<_Tp>::value...>;
|
using _CanCopyAssign = __all<is_copy_assignable<_Tp>::value...>;
|
||||||
using _CanMoveAssign = __all<is_move_assignable<_Tp>::value...>;
|
using _CanMoveAssign = __all<is_move_assignable<_Tp>::value...>;
|
||||||
|
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
tuple& operator=(typename conditional<_CanCopyAssign::value, tuple, __nat>::type const& __t)
|
tuple& operator=(typename conditional<_CanCopyAssign::value, tuple const&, __nat>::type const& __t)
|
||||||
_NOEXCEPT_((__all<is_nothrow_copy_assignable<_Tp>::value...>::value))
|
_NOEXCEPT_((__all<is_nothrow_copy_assignable<_Tp>::value...>::value))
|
||||||
{
|
{
|
||||||
base_.operator=(__t.base_);
|
base_.operator=(__t.base_);
|
||||||
@@ -889,16 +903,19 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <class _Tuple,
|
template <class _Tuple,
|
||||||
|
class _Deduced = __deduce_tuple_like<_Tuple>,
|
||||||
|
class _TupBase = typename _Deduced::_QualType,
|
||||||
class = typename enable_if
|
class = typename enable_if
|
||||||
<
|
<
|
||||||
__tuple_assignable<_Tuple, tuple>::value
|
__tuple_assignable<_TupBase, tuple>::value
|
||||||
>::type
|
>::type
|
||||||
>
|
>
|
||||||
_LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_INLINE_VISIBILITY
|
||||||
tuple&
|
tuple&
|
||||||
operator=(_Tuple&& __t) _NOEXCEPT_((is_nothrow_assignable<base&, _Tuple>::value))
|
operator=(_Tuple&& __t)
|
||||||
|
_NOEXCEPT_((is_nothrow_assignable<base&, _TupBase>::value))
|
||||||
{
|
{
|
||||||
base_.operator=(_VSTD::forward<_Tuple>(__t));
|
base_.operator=(_VSTD::forward<_TupBase>(__t));
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -4696,6 +4696,65 @@ struct __can_extract_map_key<_ValTy, _Key, _Key, _RawValTy>
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef _LIBCPP_CXX03_LANG
|
||||||
|
|
||||||
|
// __propagate_value_category -- A utility for detecting the value category
|
||||||
|
// of a given type and applying that value-category to another type.
|
||||||
|
// For example applying the cv-ref quals of a derived type to form the
|
||||||
|
// correctly cv-qualified base type.
|
||||||
|
template <class _FromType>
|
||||||
|
struct __propagate_value_category {
|
||||||
|
template <class _ToType> struct __checked_apply {
|
||||||
|
static_assert(!is_reference<_ToType>::value, "must be unqualified");
|
||||||
|
static_assert(!is_const<_ToType>::value
|
||||||
|
&& !is_volatile<_ToType>::value, "must be unqualified");
|
||||||
|
using type = _ToType;
|
||||||
|
};
|
||||||
|
template <class _ToType>
|
||||||
|
using __apply = typename __checked_apply<_ToType>::type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class _FromType>
|
||||||
|
struct __propagate_value_category<_FromType&> {
|
||||||
|
template <class _ToType>
|
||||||
|
using __apply = typename add_lvalue_reference<
|
||||||
|
typename __propagate_value_category<_FromType>::template __apply<_ToType>
|
||||||
|
>::type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class _FromType>
|
||||||
|
struct __propagate_value_category<_FromType&&> {
|
||||||
|
template <class _ToType>
|
||||||
|
using __apply = typename add_rvalue_reference<
|
||||||
|
typename __propagate_value_category<_FromType>::template __apply<_ToType>
|
||||||
|
>::type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class _FromType>
|
||||||
|
struct __propagate_value_category<_FromType const> {
|
||||||
|
template <class _ToType>
|
||||||
|
using __apply = typename add_const<
|
||||||
|
typename __propagate_value_category<_FromType>::template __apply<_ToType>
|
||||||
|
>::type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class _FromType>
|
||||||
|
struct __propagate_value_category<_FromType volatile> {
|
||||||
|
template <class _ToType>
|
||||||
|
using __apply = typename add_volatile<
|
||||||
|
typename __propagate_value_category<_FromType>::template __apply<_ToType>
|
||||||
|
>::type;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class _FromType>
|
||||||
|
struct __propagate_value_category<_FromType const volatile> {
|
||||||
|
template <class _ToType>
|
||||||
|
using __apply = typename add_cv<
|
||||||
|
typename __propagate_value_category<_FromType>::template __apply<_ToType>
|
||||||
|
>::type;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
_LIBCPP_END_NAMESPACE_STD
|
_LIBCPP_END_NAMESPACE_STD
|
||||||
|
|
||||||
#endif // _LIBCPP_TYPE_TRAITS
|
#endif // _LIBCPP_TYPE_TRAITS
|
||||||
|
|||||||
@@ -0,0 +1,113 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||||
|
// Source Licenses. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
// <tuple>
|
||||||
|
|
||||||
|
// template <class... Types> class tuple;
|
||||||
|
|
||||||
|
// template <class... UTypes>
|
||||||
|
// tuple& operator=(const tuple<UTypes...>& u);
|
||||||
|
|
||||||
|
// UNSUPPORTED: c++98, c++03
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <array>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "propagate_value_category.hpp"
|
||||||
|
|
||||||
|
struct TracksIntQuals {
|
||||||
|
TracksIntQuals() : value(-1), value_category(VC_None), assigned(false) {}
|
||||||
|
|
||||||
|
template <class Tp,
|
||||||
|
class = typename std::enable_if<!std::is_same<
|
||||||
|
typename std::decay<Tp>::type, TracksIntQuals>::value>::type>
|
||||||
|
TracksIntQuals(Tp &&x)
|
||||||
|
: value(x), value_category(getValueCategory<Tp &&>()), assigned(false) {
|
||||||
|
static_assert(std::is_same<UnCVRef<Tp>, int>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Tp,
|
||||||
|
class = typename std::enable_if<!std::is_same<
|
||||||
|
typename std::decay<Tp>::type, TracksIntQuals>::value>::type>
|
||||||
|
TracksIntQuals &operator=(Tp &&x) {
|
||||||
|
static_assert(std::is_same<UnCVRef<Tp>, int>::value, "");
|
||||||
|
value = x;
|
||||||
|
value_category = getValueCategory<Tp &&>();
|
||||||
|
assigned = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
value = -1;
|
||||||
|
value_category = VC_None;
|
||||||
|
assigned = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkConstruct(int expect, ValueCategory expect_vc) const {
|
||||||
|
return value != 1 && value == expect && value_category == expect_vc &&
|
||||||
|
assigned == false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkAssign(int expect, ValueCategory expect_vc) const {
|
||||||
|
return value != 1 && value == expect && value_category == expect_vc &&
|
||||||
|
assigned == true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int value;
|
||||||
|
ValueCategory value_category;
|
||||||
|
bool assigned;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class Tup>
|
||||||
|
struct DerivedFromTup : Tup {
|
||||||
|
using Tup::Tup;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <ValueCategory VC>
|
||||||
|
void do_derived_assign_test() {
|
||||||
|
using Tup1 = std::tuple<long, TracksIntQuals>;
|
||||||
|
Tup1 t;
|
||||||
|
auto reset = [&]() {
|
||||||
|
std::get<0>(t) = -1;
|
||||||
|
std::get<1>(t).reset();
|
||||||
|
};
|
||||||
|
{
|
||||||
|
DerivedFromTup<std::tuple<int, int>> d(42, 101);
|
||||||
|
t = ValueCategoryCast<VC>(d);
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkAssign(101, VC));
|
||||||
|
}
|
||||||
|
reset();
|
||||||
|
{
|
||||||
|
DerivedFromTup<std::pair<int, int>> d(42, 101);
|
||||||
|
t = ValueCategoryCast<VC>(d);
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkAssign(101, VC));
|
||||||
|
}
|
||||||
|
reset();
|
||||||
|
{
|
||||||
|
DerivedFromTup<std::array<int, 2>> d = {{{42, 101}}};
|
||||||
|
t = ValueCategoryCast<VC>(d);
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkAssign(101, VC));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
do_derived_assign_test<VC_LVal | VC_Const>();
|
||||||
|
do_derived_assign_test<VC_RVal>();
|
||||||
|
#if defined(_LIBCPP_VERSION)
|
||||||
|
// Non-const copy assign and const move assign are libc++ extensions.
|
||||||
|
do_derived_assign_test<VC_LVal>();
|
||||||
|
do_derived_assign_test<VC_RVal | VC_Const>();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
@@ -0,0 +1,150 @@
|
|||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
//
|
||||||
|
// The LLVM Compiler Infrastructure
|
||||||
|
//
|
||||||
|
// This file is dual licensed under the MIT and the University of Illinois Open
|
||||||
|
// Source Licenses. See LICENSE.TXT for details.
|
||||||
|
//
|
||||||
|
//===----------------------------------------------------------------------===//
|
||||||
|
|
||||||
|
// <tuple>
|
||||||
|
|
||||||
|
// template <class... Types> class tuple;
|
||||||
|
|
||||||
|
// template <class... UTypes>
|
||||||
|
// tuple& operator=(const tuple<UTypes...>& u);
|
||||||
|
|
||||||
|
// UNSUPPORTED: c++98, c++03
|
||||||
|
|
||||||
|
#include <tuple>
|
||||||
|
#include <array>
|
||||||
|
#include <string>
|
||||||
|
#include <utility>
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
#include "propagate_value_category.hpp"
|
||||||
|
|
||||||
|
template <bool Explicit = false>
|
||||||
|
struct TracksIntQuals {
|
||||||
|
TracksIntQuals() : value(-1), value_category(VC_None), assigned(false) {}
|
||||||
|
|
||||||
|
template <
|
||||||
|
class Tp,
|
||||||
|
typename std::enable_if<Explicit &&
|
||||||
|
!std::is_same<typename std::decay<Tp>::type,
|
||||||
|
TracksIntQuals>::value,
|
||||||
|
bool>::type = false>
|
||||||
|
explicit TracksIntQuals(Tp &&x)
|
||||||
|
: value(x), value_category(getValueCategory<Tp &&>()), assigned(false) {
|
||||||
|
static_assert(std::is_same<UnCVRef<Tp>, int>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
class Tp,
|
||||||
|
typename std::enable_if<!Explicit &&
|
||||||
|
!std::is_same<typename std::decay<Tp>::type,
|
||||||
|
TracksIntQuals>::value,
|
||||||
|
bool>::type = false>
|
||||||
|
TracksIntQuals(Tp &&x)
|
||||||
|
: value(x), value_category(getValueCategory<Tp &&>()), assigned(false) {
|
||||||
|
static_assert(std::is_same<UnCVRef<Tp>, int>::value, "");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Tp,
|
||||||
|
class = typename std::enable_if<!std::is_same<
|
||||||
|
typename std::decay<Tp>::type, TracksIntQuals>::value>::type>
|
||||||
|
TracksIntQuals &operator=(Tp &&x) {
|
||||||
|
static_assert(std::is_same<UnCVRef<Tp>, int>::value, "");
|
||||||
|
value = x;
|
||||||
|
value_category = getValueCategory<Tp &&>();
|
||||||
|
assigned = true;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
value = -1;
|
||||||
|
value_category = VC_None;
|
||||||
|
assigned = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkConstruct(int expect, ValueCategory expect_vc) const {
|
||||||
|
return value != 1 && value == expect && value_category == expect_vc &&
|
||||||
|
assigned == false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool checkAssign(int expect, ValueCategory expect_vc) const {
|
||||||
|
return value != 1 && value == expect && value_category == expect_vc &&
|
||||||
|
assigned == true;
|
||||||
|
}
|
||||||
|
|
||||||
|
int value;
|
||||||
|
ValueCategory value_category;
|
||||||
|
bool assigned;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <class Tup>
|
||||||
|
struct DerivedFromTup : Tup {
|
||||||
|
using Tup::Tup;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <ValueCategory VC>
|
||||||
|
void do_derived_construct_test() {
|
||||||
|
using Tup1 = std::tuple<long, TracksIntQuals</*Explicit*/ false>>;
|
||||||
|
{
|
||||||
|
DerivedFromTup<std::tuple<int, int>> d(42, 101);
|
||||||
|
Tup1 t = ValueCategoryCast<VC>(d);
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkConstruct(101, VC));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
DerivedFromTup<std::pair<int, int>> d(42, 101);
|
||||||
|
Tup1 t = ValueCategoryCast<VC>(d);
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkConstruct(101, VC));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
DerivedFromTup<std::array<int, 2>> d = {{{42, 101}}};
|
||||||
|
Tup1 t = ValueCategoryCast<VC>(d);
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkConstruct(101, VC));
|
||||||
|
}
|
||||||
|
|
||||||
|
using Tup2 = std::tuple<long, TracksIntQuals</*Explicit*/ true>>;
|
||||||
|
{
|
||||||
|
using D = DerivedFromTup<std::tuple<int, int>>;
|
||||||
|
static_assert(!std::is_convertible<ApplyValueCategoryT<VC, D>, Tup2>::value,
|
||||||
|
"");
|
||||||
|
D d(42, 101);
|
||||||
|
Tup2 t(ValueCategoryCast<VC>(d));
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkConstruct(101, VC));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
using D = DerivedFromTup<std::pair<int, int>>;
|
||||||
|
static_assert(!std::is_convertible<ApplyValueCategoryT<VC, D>, Tup2>::value,
|
||||||
|
"");
|
||||||
|
D d(42, 101);
|
||||||
|
Tup2 t(ValueCategoryCast<VC>(d));
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkConstruct(101, VC));
|
||||||
|
}
|
||||||
|
{
|
||||||
|
using D = DerivedFromTup<std::array<int, 2>>;
|
||||||
|
static_assert(!std::is_convertible<ApplyValueCategoryT<VC, D>, Tup2>::value,
|
||||||
|
"");
|
||||||
|
D d = {{{42, 101}}};
|
||||||
|
Tup2 t(ValueCategoryCast<VC>(d));
|
||||||
|
assert(std::get<0>(t) == 42);
|
||||||
|
assert(std::get<1>(t).checkConstruct(101, VC));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
do_derived_construct_test<VC_LVal | VC_Const>();
|
||||||
|
do_derived_construct_test<VC_RVal>();
|
||||||
|
#if defined(_LIBCPP_VERSION)
|
||||||
|
// Supporting non-const copy and const move are libc++ extensions
|
||||||
|
do_derived_construct_test<VC_LVal>();
|
||||||
|
do_derived_construct_test<VC_RVal | VC_Const>();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
145
test/support/propagate_value_category.hpp
Normal file
145
test/support/propagate_value_category.hpp
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
#ifndef TEST_SUPPORT_PROPAGATE_VALUE_CATEGORY
|
||||||
|
#define TEST_SUPPORT_PROPAGATE_VALUE_CATEGORY
|
||||||
|
|
||||||
|
#include "test_macros.h"
|
||||||
|
#include <type_traits>
|
||||||
|
|
||||||
|
#if TEST_STD_VER < 11
|
||||||
|
#error this header may only be used in C++11
|
||||||
|
#endif
|
||||||
|
|
||||||
|
using UnderlyingVCType = unsigned;
|
||||||
|
enum ValueCategory : UnderlyingVCType {
|
||||||
|
VC_None = 0,
|
||||||
|
VC_LVal = 1 << 0,
|
||||||
|
VC_RVal = 1 << 1,
|
||||||
|
VC_Const = 1 << 2,
|
||||||
|
VC_Volatile = 1 << 3,
|
||||||
|
VC_ConstVolatile = VC_Const | VC_Volatile
|
||||||
|
};
|
||||||
|
|
||||||
|
inline constexpr ValueCategory operator&(ValueCategory LHS, ValueCategory RHS) {
|
||||||
|
return ValueCategory(LHS & (UnderlyingVCType)RHS);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline constexpr ValueCategory operator|(ValueCategory LHS, ValueCategory RHS) {
|
||||||
|
return ValueCategory(LHS | (UnderlyingVCType)RHS);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline constexpr ValueCategory operator^(ValueCategory LHS, ValueCategory RHS) {
|
||||||
|
return ValueCategory(LHS ^ (UnderlyingVCType)RHS);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline constexpr bool isValidValueCategory(ValueCategory VC) {
|
||||||
|
return (VC & (VC_LVal | VC_RVal)) != (VC_LVal | VC_RVal);
|
||||||
|
}
|
||||||
|
|
||||||
|
inline constexpr bool hasValueCategory(ValueCategory Arg, ValueCategory Key) {
|
||||||
|
return Arg == Key || ((Arg & Key) == Key);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Tp>
|
||||||
|
using UnCVRef =
|
||||||
|
typename std::remove_cv<typename std::remove_reference<Tp>::type>::type;
|
||||||
|
|
||||||
|
template <class Tp>
|
||||||
|
constexpr ValueCategory getReferenceQuals() {
|
||||||
|
return std::is_lvalue_reference<Tp>::value
|
||||||
|
? VC_LVal
|
||||||
|
: (std::is_rvalue_reference<Tp>::value ? VC_RVal : VC_None);
|
||||||
|
}
|
||||||
|
static_assert(getReferenceQuals<int>() == VC_None, "");
|
||||||
|
static_assert(getReferenceQuals<int &>() == VC_LVal, "");
|
||||||
|
static_assert(getReferenceQuals<int &&>() == VC_RVal, "");
|
||||||
|
|
||||||
|
template <class Tp>
|
||||||
|
constexpr ValueCategory getCVQuals() {
|
||||||
|
using Vp = typename std::remove_reference<Tp>::type;
|
||||||
|
return std::is_const<Vp>::value && std::is_volatile<Vp>::value
|
||||||
|
? VC_ConstVolatile
|
||||||
|
: (std::is_const<Vp>::value
|
||||||
|
? VC_Const
|
||||||
|
: (std::is_volatile<Vp>::value ? VC_Volatile : VC_None));
|
||||||
|
}
|
||||||
|
static_assert(getCVQuals<int>() == VC_None, "");
|
||||||
|
static_assert(getCVQuals<int const>() == VC_Const, "");
|
||||||
|
static_assert(getCVQuals<int volatile>() == VC_Volatile, "");
|
||||||
|
static_assert(getCVQuals<int const volatile>() == VC_ConstVolatile, "");
|
||||||
|
static_assert(getCVQuals<int &>() == VC_None, "");
|
||||||
|
static_assert(getCVQuals<int const &>() == VC_Const, "");
|
||||||
|
|
||||||
|
template <class Tp>
|
||||||
|
inline constexpr ValueCategory getValueCategory() {
|
||||||
|
return getReferenceQuals<Tp>() | getCVQuals<Tp>();
|
||||||
|
}
|
||||||
|
static_assert(getValueCategory<int>() == VC_None, "");
|
||||||
|
static_assert(getValueCategory<int const &>() == (VC_LVal | VC_Const), "");
|
||||||
|
static_assert(getValueCategory<int const volatile &&>() ==
|
||||||
|
(VC_RVal | VC_ConstVolatile),
|
||||||
|
"");
|
||||||
|
|
||||||
|
template <ValueCategory VC>
|
||||||
|
struct ApplyValueCategory {
|
||||||
|
private:
|
||||||
|
static_assert(isValidValueCategory(VC), "");
|
||||||
|
|
||||||
|
template <bool Pred, class Then, class Else>
|
||||||
|
using CondT = typename std::conditional<Pred, Then, Else>::type;
|
||||||
|
|
||||||
|
public:
|
||||||
|
template <class Tp, class Vp = UnCVRef<Tp>>
|
||||||
|
using ApplyCVQuals = CondT<
|
||||||
|
hasValueCategory(VC, VC_ConstVolatile), typename std::add_cv<Vp>::type,
|
||||||
|
CondT<hasValueCategory(VC, VC_Const), typename std::add_const<Vp>::type,
|
||||||
|
CondT<hasValueCategory(VC, VC_Volatile),
|
||||||
|
typename std::add_volatile<Vp>::type, Tp>>>;
|
||||||
|
|
||||||
|
template <class Tp, class Vp = typename std::remove_reference<Tp>::type>
|
||||||
|
using ApplyReferenceQuals =
|
||||||
|
CondT<hasValueCategory(VC, VC_LVal),
|
||||||
|
typename std::add_lvalue_reference<Vp>::type,
|
||||||
|
CondT<hasValueCategory(VC, VC_RVal),
|
||||||
|
typename std::add_rvalue_reference<Vp>::type, Vp>>;
|
||||||
|
|
||||||
|
template <class Tp>
|
||||||
|
using Apply = ApplyReferenceQuals<ApplyCVQuals<UnCVRef<Tp>>>;
|
||||||
|
|
||||||
|
template <class Tp, bool Dummy = true,
|
||||||
|
typename std::enable_if<Dummy && (VC & VC_LVal), bool>::type = true>
|
||||||
|
static Apply<UnCVRef<Tp>> cast(Tp &&t) {
|
||||||
|
using ToType = Apply<UnCVRef<Tp>>;
|
||||||
|
return static_cast<ToType>(t);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <class Tp, bool Dummy = true,
|
||||||
|
typename std::enable_if<Dummy && (VC & VC_RVal), bool>::type = true>
|
||||||
|
static Apply<UnCVRef<Tp>> cast(Tp &&t) {
|
||||||
|
using ToType = Apply<UnCVRef<Tp>>;
|
||||||
|
return static_cast<ToType>(std::move(t));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
class Tp, bool Dummy = true,
|
||||||
|
typename std::enable_if<Dummy && ((VC & (VC_LVal | VC_RVal)) == VC_None),
|
||||||
|
bool>::type = true>
|
||||||
|
static Apply<UnCVRef<Tp>> cast(Tp &&t) {
|
||||||
|
return t;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
template <ValueCategory VC, class Tp>
|
||||||
|
using ApplyValueCategoryT = typename ApplyValueCategory<VC>::template Apply<Tp>;
|
||||||
|
|
||||||
|
template <class Tp>
|
||||||
|
using PropagateValueCategory = ApplyValueCategory<getValueCategory<Tp>()>;
|
||||||
|
|
||||||
|
template <class Tp, class Up>
|
||||||
|
using PropagateValueCategoryT =
|
||||||
|
typename ApplyValueCategory<getValueCategory<Tp>()>::template Apply<Up>;
|
||||||
|
|
||||||
|
template <ValueCategory VC, class Tp>
|
||||||
|
typename ApplyValueCategory<VC>::template Apply<Tp> ValueCategoryCast(Tp &&t) {
|
||||||
|
return ApplyValueCategory<VC>::cast(std::forward<Tp>(t));
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // TEST_SUPPORT_PROPAGATE_VALUE_CATEGORY
|
||||||
Reference in New Issue
Block a user