Skip to content

Commit e03c435

Browse files
[libc++] Fix tuple_cat for element with unconstrained constructor (#122433)
Currently, when the result type is 1-`tuple`, `tuple_cat` possibly tests an undesired constructor of the element, due to conversion from the reference tuple to the result type. If the element type has an unconstrained constructor template, there can be extraneous hard error which shouldn't happen. This patch introduces a helper function template to select the element-wise constructor template of `tuple`, which can avoid such error. Fixes #41034.
1 parent 438e2cc commit e03c435

File tree

2 files changed

+67
-4
lines changed

2 files changed

+67
-4
lines changed

libcxx/include/tuple

+16-3
Original file line numberDiff line numberDiff line change
@@ -1338,12 +1338,25 @@ struct __tuple_cat<tuple<_Types...>, __tuple_indices<_I0...>, __tuple_indices<_J
13381338
}
13391339
};
13401340

1341+
template <class _TupleDst, class _TupleSrc, size_t... _Indices>
1342+
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 _TupleDst
1343+
__tuple_cat_select_element_wise(_TupleSrc&& __src, __tuple_indices<_Indices...>) {
1344+
static_assert(tuple_size<_TupleDst>::value == tuple_size<_TupleSrc>::value,
1345+
"misuse of __tuple_cat_select_element_wise with tuples of different sizes");
1346+
return _TupleDst(std::get<_Indices>(std::forward<_TupleSrc>(__src))...);
1347+
}
1348+
13411349
template <class _Tuple0, class... _Tuples>
13421350
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR_SINCE_CXX14 typename __tuple_cat_return<_Tuple0, _Tuples...>::type
13431351
tuple_cat(_Tuple0&& __t0, _Tuples&&... __tpls) {
1344-
using _T0 _LIBCPP_NODEBUG = __libcpp_remove_reference_t<_Tuple0>;
1345-
return __tuple_cat<tuple<>, __tuple_indices<>, typename __make_tuple_indices<tuple_size<_T0>::value>::type>()(
1346-
tuple<>(), std::forward<_Tuple0>(__t0), std::forward<_Tuples>(__tpls)...);
1352+
using _T0 _LIBCPP_NODEBUG = __libcpp_remove_reference_t<_Tuple0>;
1353+
using _TRet _LIBCPP_NODEBUG = typename __tuple_cat_return<_Tuple0, _Tuples...>::type;
1354+
using _T0Indices _LIBCPP_NODEBUG = typename __make_tuple_indices<tuple_size<_T0>::value>::type;
1355+
using _TRetIndices _LIBCPP_NODEBUG = typename __make_tuple_indices<tuple_size<_TRet>::value>::type;
1356+
return std::__tuple_cat_select_element_wise<_TRet>(
1357+
__tuple_cat<tuple<>, __tuple_indices<>, _T0Indices>()(
1358+
tuple<>(), std::forward<_Tuple0>(__t0), std::forward<_Tuples>(__tpls)...),
1359+
_TRetIndices());
13471360
}
13481361

13491362
template <class... _Tp, class _Alloc>

libcxx/test/std/utilities/tuple/tuple.tuple/tuple.creation/tuple_cat.pass.cpp

+51-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,48 @@ template<typename ...Ts>
3131
void forward_as_tuple(Ts...) = delete;
3232
}
3333

34+
// https://github.com/llvm/llvm-project/issues/41034
35+
struct Unconstrained {
36+
int data;
37+
template <typename Arg>
38+
TEST_CONSTEXPR_CXX14 Unconstrained(Arg arg) : data(arg) {}
39+
};
40+
41+
TEST_CONSTEXPR_CXX14 bool test_tuple_cat_with_unconstrained_constructor() {
42+
{
43+
auto tup_src = std::tuple<Unconstrained>(Unconstrained(5));
44+
auto tup = std::tuple_cat(tup_src);
45+
assert(std::get<0>(tup).data == 5);
46+
}
47+
{
48+
auto tup = std::tuple_cat(std::tuple<Unconstrained>(Unconstrained(6)));
49+
assert(std::get<0>(tup).data == 6);
50+
}
51+
{
52+
auto tup = std::tuple_cat(std::tuple<Unconstrained>(Unconstrained(7)), std::tuple<>());
53+
assert(std::get<0>(tup).data == 7);
54+
}
55+
#if TEST_STD_VER >= 17
56+
{
57+
auto tup_src = std::tuple(Unconstrained(8));
58+
auto tup = std::tuple_cat(tup_src);
59+
ASSERT_SAME_TYPE(decltype(tup), std::tuple<Unconstrained>);
60+
assert(std::get<0>(tup).data == 8);
61+
}
62+
{
63+
auto tup = std::tuple_cat(std::tuple(Unconstrained(9)));
64+
ASSERT_SAME_TYPE(decltype(tup), std::tuple<Unconstrained>);
65+
assert(std::get<0>(tup).data == 9);
66+
}
67+
{
68+
auto tup = std::tuple_cat(std::tuple(Unconstrained(10)), std::tuple());
69+
ASSERT_SAME_TYPE(decltype(tup), std::tuple<Unconstrained>);
70+
assert(std::get<0>(tup).data == 10);
71+
}
72+
#endif
73+
return true;
74+
}
75+
3476
int main(int, char**)
3577
{
3678
{
@@ -270,5 +312,13 @@ int main(int, char**)
270312
assert(std::get<0>(t).i == 1);
271313
assert(std::get<0>(t2).i == 1);
272314
}
273-
return 0;
315+
// See https://github.com/llvm/llvm-project/issues/41034
316+
{
317+
test_tuple_cat_with_unconstrained_constructor();
318+
#if TEST_STD_VER >= 14
319+
static_assert(test_tuple_cat_with_unconstrained_constructor(), "");
320+
#endif
321+
}
322+
323+
return 0;
274324
}

0 commit comments

Comments
 (0)