Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[libc++] Fix tuple_cat for element with unconstrained constructor #122433

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions libcxx/include/tuple
Original file line number Diff line number Diff line change
Expand Up @@ -1338,12 +1338,25 @@ struct __tuple_cat<tuple<_Types...>, __tuple_indices<_I0...>, __tuple_indices<_J
}
};

template <class _TupleDst, class _TupleSrc, size_t... _Indices>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _TupleDst
__tuple_cat_select_element_wise(_TupleSrc&& __src, __tuple_indices<_Indices...>) {
static_assert(tuple_size<_TupleDst>::value == tuple_size<_TupleSrc>::value,
"misuse of __tuple_cat_select_element_wise with tuples of different sizes");
return _TupleDst(std::get<_Indices>(std::forward<_TupleSrc>(__src))...);
}

template <class _Tuple0, class... _Tuples>
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __tuple_cat_return<_Tuple0, _Tuples...>::type
tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls) {
using _T0 _LIBCPP_NODEBUG = __libcpp_remove_reference_t<_Tuple0>;
return __tuple_cat<tuple<>, __tuple_indices<>, typename __make_tuple_indices<tuple_size<_T0>::value>::type>()(
tuple<>(), std::forward<_Tuple0>(__t0), std::forward<_Tuples>(__tpls)...);
using _T0 _LIBCPP_NODEBUG = __libcpp_remove_reference_t<_Tuple0>;
using _TRet _LIBCPP_NODEBUG = typename __tuple_cat_return<_Tuple0, _Tuples...>::type;
using _T0Indices _LIBCPP_NODEBUG = typename __make_tuple_indices<tuple_size<_T0>::value>::type;
using _TRetIndices _LIBCPP_NODEBUG = typename __make_tuple_indices<tuple_size<_TRet>::value>::type;
return std::__tuple_cat_select_element_wise<_TRet>(
__tuple_cat<tuple<>, __tuple_indices<>, _T0Indices>()(
tuple<>(), std::forward<_Tuple0>(__t0), std::forward<_Tuples>(__tpls)...),
_TRetIndices());
}

template <class... _Tp, class _Alloc>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,48 @@ template<typename ...Ts>
void forward_as_tuple(Ts...) = delete;
}

// https://github.com/llvm/llvm-project/issues/41034
struct Unconstrained {
int data;
template <typename Arg>
TEST_CONSTEXPR_CXX14 Unconstrained(Arg arg) : data(arg) {}
};

TEST_CONSTEXPR_CXX14 bool test_tuple_cat_with_unconstrained_constructor() {
{
auto tup_src = std::tuple<Unconstrained>(Unconstrained(5));
auto tup = std::tuple_cat(tup_src);
assert(std::get<0>(tup).data == 5);
}
{
auto tup = std::tuple_cat(std::tuple<Unconstrained>(Unconstrained(6)));
assert(std::get<0>(tup).data == 6);
}
{
auto tup = std::tuple_cat(std::tuple<Unconstrained>(Unconstrained(7)), std::tuple<>());
assert(std::get<0>(tup).data == 7);
}
#if TEST_STD_VER >= 17
{
auto tup_src = std::tuple(Unconstrained(8));
auto tup = std::tuple_cat(tup_src);
ASSERT_SAME_TYPE(decltype(tup), std::tuple<Unconstrained>);
assert(std::get<0>(tup).data == 8);
}
{
auto tup = std::tuple_cat(std::tuple(Unconstrained(9)));
ASSERT_SAME_TYPE(decltype(tup), std::tuple<Unconstrained>);
assert(std::get<0>(tup).data == 9);
}
{
auto tup = std::tuple_cat(std::tuple(Unconstrained(10)), std::tuple());
ASSERT_SAME_TYPE(decltype(tup), std::tuple<Unconstrained>);
assert(std::get<0>(tup).data == 10);
}
#endif
return true;
}

int main(int, char**)
{
{
Expand Down Expand Up @@ -270,5 +312,13 @@ int main(int, char**)
assert(std::get<0>(t).i == 1);
assert(std::get<0>(t2).i == 1);
}
return 0;
// See https://github.com/llvm/llvm-project/issues/41034
{
test_tuple_cat_with_unconstrained_constructor();
#if TEST_STD_VER >= 14
static_assert(test_tuple_cat_with_unconstrained_constructor(), "");
#endif
}

return 0;
}
Loading