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

<tuple>: Make std::make_from_tuple SFINAE friendly #4528

Merged
merged 28 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
3416105
<tuple>: Make std::make_from_tuple and std::_Make_from_tuple_impl SFI…
yronglin Mar 24, 2024
731c998
Format
yronglin Mar 24, 2024
959808d
Format
yronglin Mar 24, 2024
e41ba98
Revert incorrect change
yronglin Mar 24, 2024
2133da6
<tuple>: Address review comments and add more test
yronglin Mar 25, 2024
1276f61
Format
yronglin Mar 25, 2024
83347f0
Address review comments and format
yronglin Mar 26, 2024
13a6ab6
Add new test to tests/std/test.lst.
StephanTLavavej Mar 26, 2024
f7ff18c
Mention libc++'s test files.
StephanTLavavej Mar 26, 2024
ab8644f
Add `std::` qualification to `<cstdint>` types.
StephanTLavavej Mar 26, 2024
3073394
Style: Use empty braces for `std::uint8_t{}` temporaries.
StephanTLavavej Mar 26, 2024
ab20411
Directly construct `make_index_sequence<MEOW>{}`, no need for `declval`.
StephanTLavavej Mar 26, 2024
26ef5d2
Rename `_Ugly` identifiers: `_Ty` => `T`
StephanTLavavej Mar 26, 2024
d65d9e6
Rename `_Ugly` identifiers: `_Tuple` => `Tuple`
StephanTLavavej Mar 26, 2024
7c99e8b
Rename `_Ugly` identifiers: `_Indices` => `Indices`
StephanTLavavej Mar 26, 2024
a5b121a
Rename `_Ugly` identifiers: `_Seq` => `Seq`
StephanTLavavej Mar 26, 2024
fd217a5
Nitpick: `struct` inheritance is already `public` by default.
StephanTLavavej Mar 26, 2024
1d9bdad
Avoid potentially-confusing `&&` in comments.
StephanTLavavej Mar 26, 2024
48f81d6
Fix comment typo (extra underscore).
StephanTLavavej Mar 26, 2024
9146dfb
`B` and `C` were unused.
StephanTLavavej Mar 26, 2024
920c5d6
Drop old-style (function-based) SFINAE test coverage.
StephanTLavavej Mar 26, 2024
37b1319
No longer need `<cstdint>` and "partial specialization" comments.
StephanTLavavej Mar 26, 2024
55bd07c
`class Seq` and `size_t... Indices` were unused.
StephanTLavavej Mar 26, 2024
60c6564
Restructure `make_from_tuple` into 23/20/17 cases.
StephanTLavavej Mar 26, 2024
ba03dc9
Use a default template argument for `_Make_from_tuple_impl`, instead …
StephanTLavavej Mar 26, 2024
b5b95a7
Restructure `_Make_from_tuple_impl` into 23/20/17 cases.
StephanTLavavej Mar 26, 2024
4e20186
Drop ALL constraints on `_Make_from_tuple_impl`, then drop test cover…
StephanTLavavej Mar 26, 2024
d05eddd
Drop `enable_if_t` layer within `_Can_make_from_tuple`.
StephanTLavavej Mar 26, 2024
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
21 changes: 12 additions & 9 deletions stl/inc/tuple
Original file line number Diff line number Diff line change
Expand Up @@ -1079,24 +1079,27 @@ constexpr decltype(auto) apply(_Callable&& _Obj, _Tuple&& _Tpl) noexcept(
make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>{});
}

#if _HAS_CXX23
template <class _Ty, _Tuple_like _Tuple, size_t... _Indices>
#else // ^^^ _HAS_CXX23 / !_HAS_CXX23 vvv
template <class _Ty, class _Tuple, class _Seq = make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>>
inline constexpr bool _Can_make_from_tuple = false;
template <class _Ty, class _Tuple, size_t... _Indices>
inline constexpr bool _Can_make_from_tuple<_Ty, _Tuple, index_sequence<_Indices...>> =
is_constructible_v<_Ty, decltype(_STD get<_Indices>(_STD declval<_Tuple>()))...>;

template <class _Ty, class _Tuple, size_t... _Indices>
#endif // ^^^ !_HAS_CXX23 ^^^
constexpr _Ty _Make_from_tuple_impl(_Tuple&& _Tpl, index_sequence<_Indices...>) noexcept(
is_nothrow_constructible_v<_Ty, decltype(_STD get<_Indices>(_STD forward<_Tuple>(_Tpl)))...>) {
// construct _Ty from the elements of _Tpl
static_assert(is_constructible_v<_Ty, decltype(_STD get<_Indices>(_STD forward<_Tuple>(_Tpl)))...>,
"the target type must be constructible from the fields of the argument tuple (N4950 [tuple.apply]/4).");
return _Ty(_STD get<_Indices>(_STD forward<_Tuple>(_Tpl))...);
}

#if _HAS_CXX23
_EXPORT_STD template <class _Ty, _Tuple_like _Tuple>
#else // ^^^ _HAS_CXX23 / !_HAS_CXX23 vvv
requires _Can_make_from_tuple<_Ty, _Tuple>
#elif _HAS_CXX20
_EXPORT_STD template <class _Ty, class _Tuple>
#endif // ^^^ !_HAS_CXX23 ^^^
requires _Can_make_from_tuple<_Ty, _Tuple>
#else // ^^^ _HAS_CXX20 / !_HAS_CXX20 vvv
template <class _Ty, class _Tuple, enable_if_t<_Can_make_from_tuple<_Ty, _Tuple>, int> = 0>
#endif // ^^^ !_HAS_CXX20 ^^^
_NODISCARD constexpr _Ty make_from_tuple(_Tuple&& _Tpl) noexcept(noexcept(_STD _Make_from_tuple_impl<_Ty>(
_STD forward<_Tuple>(_Tpl), make_index_sequence<tuple_size_v<remove_reference_t<_Tuple>>>{}))) /* strengthened */ {
// construct _Ty from the elements of _Tpl
Expand Down
1 change: 1 addition & 0 deletions tests/std/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ tests\LWG3146_excessive_unwrapping_ref_cref
tests\LWG3234_math_special_overloads
tests\LWG3422_seed_seq_ctors
tests\LWG3480_directory_iterator_range
tests\LWG3528_make_from_tuple_impl
tests\LWG3545_pointer_traits_sfinae
tests\LWG3561_discard_block_engine_counter
tests\LWG3610_iota_view_size_and_integer_class
Expand Down
4 changes: 4 additions & 0 deletions tests/std/tests/LWG3528_make_from_tuple_impl/env.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\usual_17_matrix.lst
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//===----------------------------------------------------------------------===//

// derived from libc++'s test files:
// * std/utilities/tuple/tuple.tuple/tuple.apply/make_from_tuple.pass.cpp

#include <array>
#include <tuple>
#include <type_traits>
#include <utility>

struct A {
int a;
};

enum class D {
one,
two,
};

template <class T, class Tuple, class = void>
inline constexpr bool has_make_from_tuple = false;

template <class T, class Tuple>
inline constexpr bool
has_make_from_tuple<T, Tuple, std::void_t<decltype(std::make_from_tuple<T>(std::declval<Tuple>()))>> = true;

// Test std::make_from_tuple.

// reinterpret_cast, std::tuple<T>
static_assert(!has_make_from_tuple<int*, std::tuple<A*>>);
static_assert(has_make_from_tuple<A*, std::tuple<A*>>);

// reinterpret_cast, std::array<T, 1>
static_assert(!has_make_from_tuple<int*, std::array<A*, 1>>);
static_assert(has_make_from_tuple<A*, std::array<A*, 1>>);

// const_cast, std::tuple<T>
static_assert(!has_make_from_tuple<char*, std::tuple<const char*>>);
static_assert(!has_make_from_tuple<volatile char*, std::tuple<const volatile char*>>);
static_assert(has_make_from_tuple<volatile char*, std::tuple<volatile char*>>);
static_assert(has_make_from_tuple<char*, std::tuple<char*>>);
static_assert(has_make_from_tuple<const char*, std::tuple<char*>>);
static_assert(has_make_from_tuple<const volatile char*, std::tuple<volatile char*>>);

// const_cast, std::array<T, 1>
static_assert(!has_make_from_tuple<char*, std::array<const char*, 1>>);
static_assert(!has_make_from_tuple<volatile char*, std::array<const volatile char*, 1>>);
static_assert(has_make_from_tuple<volatile char*, std::array<volatile char*, 1>>);
static_assert(has_make_from_tuple<char*, std::array<char*, 1>>);
static_assert(has_make_from_tuple<const char*, std::array<char*, 1>>);
static_assert(has_make_from_tuple<const volatile char*, std::array<volatile char*, 1>>);

// static_cast, std::tuple<T>
static_assert(!has_make_from_tuple<int, std::tuple<D>>);
static_assert(!has_make_from_tuple<D, std::tuple<int>>);
static_assert(has_make_from_tuple<long, std::tuple<int>>);
static_assert(has_make_from_tuple<double, std::tuple<float>>);
static_assert(has_make_from_tuple<float, std::tuple<double>>);

// static_cast, std::array<T, 1>
static_assert(!has_make_from_tuple<int, std::array<D, 1>>);
static_assert(!has_make_from_tuple<D, std::array<int, 1>>);
static_assert(has_make_from_tuple<long, std::array<int, 1>>);
static_assert(has_make_from_tuple<double, std::array<float, 1>>);
static_assert(has_make_from_tuple<float, std::array<double, 1>>);