[libcxx-commits] [libcxx] [libc++] Implement LWG3528 (`make_from_tuple` can perform (the equivalent of) a C-style cast) (PR #85263)

via libcxx-commits libcxx-commits at lists.llvm.org
Mon Mar 18 06:57:47 PDT 2024


================
@@ -0,0 +1,46 @@
+//===----------------------------------------------------------------------===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+// UNSUPPORTED: c++03, c++11, c++14
----------------
yronglin wrote:

Possible implementation:
```
#if _LIBCPP_STD_VER >= 20
template <class _Tp, class _Tuple, size_t... _Idx>
inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp __make_from_tuple_impl(_Tuple&& __t, __tuple_indices<_Idx...>)
  noexcept(noexcept(_Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...)))
  requires is_constructible_v<_Tp, decltype(std::get<_Idx>(std::forward<_Tuple>(__t)))...> {
  return _Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...);
}
#else
template <class _Tp, class _Tuple, size_t... _Idx>
inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp __make_from_tuple_impl(_Tuple&& __t, __tuple_indices<_Idx...>, 
    enable_if_t<is_constructible_v<_Tp, decltype(std::get<_Idx>(std::forward<_Tuple>(__t)))...>> * = nullptr)
    _LIBCPP_NOEXCEPT_RETURN(_Tp(std::get<_Idx>(std::forward<_Tuple>(__t))...))
#endif // _LIBCPP_STD_VER >= 20

// clang-format on
template <class _Tp, class _Tuple, class = std::void_t<>>
struct __can_make_from_tuple : std::false_type {};
template <class _Tp, class _Tuple>
struct __can_make_from_tuple<
    _Tp, _Tuple,
    std::void_t<decltype(std::__make_from_tuple_impl<_Tp>(
        std::declval<_Tuple>(),
        std::declval<typename __make_tuple_indices<
            tuple_size_v<remove_reference_t<_Tuple>>>::type>()))>>
    : std::true_type {};

template <class _Tp, class _Tuple>
inline _LIBCPP_HIDE_FROM_ABI constexpr _Tp make_from_tuple(
    _Tuple &&__t,
    std::enable_if_t<__can_make_from_tuple<_Tp, _Tuple>::value> * = nullptr)
    _LIBCPP_NOEXCEPT_RETURN(std::__make_from_tuple_impl<_Tp>(
        std::forward<_Tuple>(__t),
        typename __make_tuple_indices<
            tuple_size_v<remove_reference_t<_Tuple>>>::type{}))
// clang-format off
```

https://github.com/llvm/llvm-project/pull/85263


More information about the libcxx-commits mailing list