Skip to content

Commit 04d3a6b

Browse files
authored
[libc++][modules] Add a header to forward-declare std::get (#108285)
This is necessary because e.g. ranges::elements_view uses std::get but it needs to have in scope the declaration of all the versions of std::get that exist in the library. This need is what had originally led to elements_view.h gaining an include of __fwd/complex.h, but in reality it is a more general issue that requires a canonical declration point for all the std::get variations.
1 parent 885ac29 commit 04d3a6b

File tree

6 files changed

+117
-21
lines changed

6 files changed

+117
-21
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ set(files
424424
__fwd/format.h
425425
__fwd/fstream.h
426426
__fwd/functional.h
427+
__fwd/get.h
427428
__fwd/ios.h
428429
__fwd/istream.h
429430
__fwd/mdspan.h
@@ -440,6 +441,7 @@ set(files
440441
__fwd/string_view.h
441442
__fwd/subrange.h
442443
__fwd/tuple.h
444+
__fwd/variant.h
443445
__fwd/vector.h
444446
__hash_table
445447
__ios/fpos.h

libcxx/include/__fwd/get.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//===---------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===---------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___FWD_GET_H
10+
#define _LIBCPP___FWD_GET_H
11+
12+
#include <__config>
13+
#include <__fwd/array.h>
14+
#include <__fwd/complex.h>
15+
#include <__fwd/pair.h>
16+
#include <__fwd/subrange.h>
17+
#include <__fwd/tuple.h>
18+
#include <__fwd/variant.h>
19+
20+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21+
# pragma GCC system_header
22+
#endif
23+
24+
#endif // _LIBCPP___FWD_GET_H

libcxx/include/__fwd/variant.h

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//===---------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===---------------------------------------------------------------------===//
8+
9+
#ifndef _LIBCPP___FWD_VARIANT_H
10+
#define _LIBCPP___FWD_VARIANT_H
11+
12+
#include <__config>
13+
#include <__cstddef/size_t.h>
14+
15+
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
16+
# pragma GCC system_header
17+
#endif
18+
19+
_LIBCPP_BEGIN_NAMESPACE_STD
20+
21+
#if _LIBCPP_STD_VER >= 17
22+
23+
template <class... _Types>
24+
class _LIBCPP_TEMPLATE_VIS variant;
25+
26+
template <class _Tp>
27+
struct _LIBCPP_TEMPLATE_VIS variant_size;
28+
29+
template <class _Tp>
30+
inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
31+
32+
template <size_t _Ip, class _Tp>
33+
struct _LIBCPP_TEMPLATE_VIS variant_alternative;
34+
35+
template <size_t _Ip, class _Tp>
36+
using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
37+
38+
inline constexpr size_t variant_npos = static_cast<size_t>(-1);
39+
40+
template <size_t _Ip, class... _Types>
41+
_LIBCPP_HIDE_FROM_ABI
42+
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&
43+
get(variant<_Types...>&);
44+
45+
template <size_t _Ip, class... _Types>
46+
_LIBCPP_HIDE_FROM_ABI
47+
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr variant_alternative_t<_Ip, variant<_Types...>>&&
48+
get(variant<_Types...>&&);
49+
50+
template <size_t _Ip, class... _Types>
51+
_LIBCPP_HIDE_FROM_ABI
52+
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&
53+
get(const variant<_Types...>&);
54+
55+
template <size_t _Ip, class... _Types>
56+
_LIBCPP_HIDE_FROM_ABI
57+
_LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const variant_alternative_t<_Ip, variant<_Types...>>&&
58+
get(const variant<_Types...>&&);
59+
60+
template <class _Tp, class... _Types>
61+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp& get(variant<_Types...>&);
62+
63+
template <class _Tp, class... _Types>
64+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr _Tp&& get(variant<_Types...>&&);
65+
66+
template <class _Tp, class... _Types>
67+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp& get(const variant<_Types...>&);
68+
69+
template <class _Tp, class... _Types>
70+
_LIBCPP_HIDE_FROM_ABI _LIBCPP_AVAILABILITY_THROW_BAD_VARIANT_ACCESS constexpr const _Tp&&
71+
get(const variant<_Types...>&&);
72+
73+
#endif // _LIBCPP_STD_VER >= 17
74+
75+
_LIBCPP_END_NAMESPACE_STD
76+
77+
#endif // _LIBCPP___FWD_VARIANT_H

libcxx/include/__ranges/elements_view.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <__concepts/derived_from.h>
1717
#include <__concepts/equality_comparable.h>
1818
#include <__config>
19-
#include <__fwd/complex.h>
19+
#include <__fwd/get.h>
2020
#include <__iterator/concepts.h>
2121
#include <__iterator/iterator_traits.h>
2222
#include <__ranges/access.h>

libcxx/include/module.modulemap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,15 @@ module std_private_tuple_tuple_like_no_subrange [system] {
18131813
module std_private_tuple_sfinae_helpers [system] { header "__tuple/sfinae_helpers.h" }
18141814
module std_private_tuple_tuple_element [system] { header "__tuple/tuple_element.h" }
18151815
module std_private_tuple_tuple_fwd [system] { header "__fwd/tuple.h" }
1816+
module std_private_get_fwd [system] {
1817+
header "__fwd/get.h"
1818+
export std_private_array_array_fwd
1819+
export std_private_complex_complex_fwd
1820+
export std_private_ranges_subrange_fwd
1821+
export std_private_tuple_tuple_fwd
1822+
export std_private_utility_pair_fwd
1823+
export std_private_variant_fwd
1824+
}
18161825
module std_private_tuple_tuple_indices [system] { header "__tuple/tuple_indices.h" }
18171826
module std_private_tuple_tuple_like [system] {
18181827
header "__tuple/tuple_like.h"
@@ -2103,5 +2112,6 @@ module std_private_utility_to_underlying [system] { header "__utility/
21032112
module std_private_utility_unreachable [system] { header "__utility/unreachable.h" }
21042113

21052114
module std_private_variant_monostate [system] { header "__variant/monostate.h" }
2115+
module std_private_variant_fwd [system] { header "__fwd/variant.h" }
21062116

21072117
module std_private_vector_fwd [system] { header "__fwd/vector.h" }

libcxx/include/variant

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,7 @@ namespace std {
221221
#include <__functional/invoke.h>
222222
#include <__functional/operations.h>
223223
#include <__functional/unary_function.h>
224+
#include <__fwd/variant.h>
224225
#include <__memory/addressof.h>
225226
#include <__memory/construct_at.h>
226227
#include <__tuple/find_index.h>
@@ -307,15 +308,7 @@ __throw_bad_variant_access() {
307308
# endif
308309
}
309310

310-
template <class... _Types>
311-
class _LIBCPP_TEMPLATE_VIS variant;
312-
313-
template <class _Tp>
314-
struct _LIBCPP_TEMPLATE_VIS variant_size;
315-
316-
template <class _Tp>
317-
inline constexpr size_t variant_size_v = variant_size<_Tp>::value;
318-
311+
// variant_size
319312
template <class _Tp>
320313
struct _LIBCPP_TEMPLATE_VIS variant_size<const _Tp> : variant_size<_Tp> {};
321314

@@ -328,12 +321,7 @@ struct _LIBCPP_TEMPLATE_VIS variant_size<const volatile _Tp> : variant_size<_Tp>
328321
template <class... _Types>
329322
struct _LIBCPP_TEMPLATE_VIS variant_size<variant<_Types...>> : integral_constant<size_t, sizeof...(_Types)> {};
330323

331-
template <size_t _Ip, class _Tp>
332-
struct _LIBCPP_TEMPLATE_VIS variant_alternative;
333-
334-
template <size_t _Ip, class _Tp>
335-
using variant_alternative_t = typename variant_alternative<_Ip, _Tp>::type;
336-
324+
// variant_alternative
337325
template <size_t _Ip, class _Tp>
338326
struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, const _Tp> : add_const<variant_alternative_t<_Ip, _Tp>> {};
339327

@@ -349,8 +337,6 @@ struct _LIBCPP_TEMPLATE_VIS variant_alternative<_Ip, variant<_Types...>> {
349337
using type = __type_pack_element<_Ip, _Types...>;
350338
};
351339

352-
inline constexpr size_t variant_npos = static_cast<size_t>(-1);
353-
354340
template <size_t _NumAlternatives>
355341
_LIBCPP_HIDE_FROM_ABI constexpr auto __choose_index_type() {
356342
# ifdef _LIBCPP_ABI_VARIANT_INDEX_TYPE_OPTIMIZATION
@@ -369,9 +355,6 @@ using __variant_index_t = decltype(std::__choose_index_type<_NumAlts>());
369355
template <class _IndexType>
370356
constexpr _IndexType __variant_npos = static_cast<_IndexType>(-1);
371357

372-
template <class... _Types>
373-
class _LIBCPP_TEMPLATE_VIS variant;
374-
375358
template <class... _Types>
376359
_LIBCPP_HIDE_FROM_ABI constexpr variant<_Types...>& __as_variant(variant<_Types...>& __vs) noexcept {
377360
return __vs;

0 commit comments

Comments
 (0)