Skip to content

Commit 40a01d5

Browse files
committed
Implement most of P0607: Inline Variables for the Standard Library. This involved marking a lot of variables as inline (but only for C++17 and later).
llvm-svn: 321658
1 parent 2640a0a commit 40a01d5

File tree

15 files changed

+295
-217
lines changed

15 files changed

+295
-217
lines changed

libcxx/include/__config

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,9 +980,10 @@ template <unsigned> struct __static_assert_check {};
980980
#define _LIBCPP_NODISCARD_AFTER_CXX17
981981
#endif
982982

983-
// FIXME: Remove all usages of this macro once compilers catch up.
984-
#if !defined(__cpp_inline_variables) || (__cpp_inline_variables < 201606L)
985-
# define _LIBCPP_HAS_NO_INLINE_VARIABLES
983+
#if _LIBCPP_STD_VER > 14 && defined(__cpp_inline_variables) && (__cpp_inline_variables >= 201606L)
984+
# define _LIBCPP_INLINE_VAR inline
985+
#else
986+
# define _LIBCPP_INLINE_VAR
986987
#endif
987988

988989
#ifdef _LIBCPP_HAS_NO_RVALUE_REFERENCES

libcxx/include/__functional_base

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ struct _LIBCPP_TEMPLATE_VIS allocator_arg_t { };
564564
#if defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_MEMORY)
565565
extern const allocator_arg_t allocator_arg;
566566
#else
567-
constexpr allocator_arg_t allocator_arg = allocator_arg_t();
567+
_LIBCPP_INLINE_VAR constexpr allocator_arg_t allocator_arg = allocator_arg_t();
568568
#endif
569569

570570
// uses_allocator
@@ -601,7 +601,7 @@ struct _LIBCPP_TEMPLATE_VIS uses_allocator
601601

602602
#if _LIBCPP_STD_VER > 14
603603
template <class _Tp, class _Alloc>
604-
constexpr size_t uses_allocator_v = uses_allocator<_Tp, _Alloc>::value;
604+
_LIBCPP_INLINE_VAR constexpr size_t uses_allocator_v = uses_allocator<_Tp, _Alloc>::value;
605605
#endif
606606

607607
#ifndef _LIBCPP_CXX03_LANG

libcxx/include/__mutex_base

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,9 @@ extern const adopt_lock_t adopt_lock;
8282

8383
#else
8484

85-
constexpr defer_lock_t defer_lock = defer_lock_t();
86-
constexpr try_to_lock_t try_to_lock = try_to_lock_t();
87-
constexpr adopt_lock_t adopt_lock = adopt_lock_t();
85+
_LIBCPP_INLINE_VAR constexpr defer_lock_t defer_lock = defer_lock_t();
86+
_LIBCPP_INLINE_VAR constexpr try_to_lock_t try_to_lock = try_to_lock_t();
87+
_LIBCPP_INLINE_VAR constexpr adopt_lock_t adopt_lock = adopt_lock_t();
8888

8989
#endif
9090

libcxx/include/chrono

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ duration_cast(const duration<Rep, Period>& fd);
2626
2727
template <class Rep> struct treat_as_floating_point : is_floating_point<Rep> {};
2828
29-
template <class Rep> constexpr bool treat_as_floating_point_v
29+
template <class Rep> inline constexpr bool treat_as_floating_point_v
3030
= treat_as_floating_point<Rep>::value; // C++17
3131
3232
template <class Rep>
@@ -419,7 +419,8 @@ template <class _Rep>
419419
struct _LIBCPP_TEMPLATE_VIS treat_as_floating_point : is_floating_point<_Rep> {};
420420

421421
#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
422-
template <class _Rep> _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
422+
template <class _Rep>
423+
_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool treat_as_floating_point_v
423424
= treat_as_floating_point<_Rep>::value;
424425
#endif
425426

libcxx/include/functional

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,9 @@ template<class T> struct is_bind_expression;
213213
template<class T> struct is_placeholder;
214214
215215
// See C++14 20.9.9, Function object binders
216-
template <class T> constexpr bool is_bind_expression_v
216+
template <class T> inline constexpr bool is_bind_expression_v
217217
= is_bind_expression<T>::value; // C++17
218-
template <class T> constexpr int is_placeholder_v
218+
template <class T> inline constexpr int is_placeholder_v
219219
= is_placeholder<T>::value; // C++17
220220
221221
@@ -1991,7 +1991,7 @@ template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_bind_expression
19911991

19921992
#if _LIBCPP_STD_VER > 14
19931993
template <class _Tp>
1994-
constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
1994+
_LIBCPP_INLINE_VAR constexpr size_t is_bind_expression_v = is_bind_expression<_Tp>::value;
19951995
#endif
19961996

19971997
template<class _Tp> struct __is_placeholder : public integral_constant<int, 0> {};
@@ -2000,7 +2000,7 @@ template<class _Tp> struct _LIBCPP_TEMPLATE_VIS is_placeholder
20002000

20012001
#if _LIBCPP_STD_VER > 14
20022002
template <class _Tp>
2003-
constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
2003+
_LIBCPP_INLINE_VAR constexpr size_t is_placeholder_v = is_placeholder<_Tp>::value;
20042004
#endif
20052005

20062006
namespace placeholders
@@ -2020,16 +2020,16 @@ _LIBCPP_FUNC_VIS extern const __ph<8> _8;
20202020
_LIBCPP_FUNC_VIS extern const __ph<9> _9;
20212021
_LIBCPP_FUNC_VIS extern const __ph<10> _10;
20222022
#else
2023-
constexpr __ph<1> _1{};
2024-
constexpr __ph<2> _2{};
2025-
constexpr __ph<3> _3{};
2026-
constexpr __ph<4> _4{};
2027-
constexpr __ph<5> _5{};
2028-
constexpr __ph<6> _6{};
2029-
constexpr __ph<7> _7{};
2030-
constexpr __ph<8> _8{};
2031-
constexpr __ph<9> _9{};
2032-
constexpr __ph<10> _10{};
2023+
_LIBCPP_INLINE_VAR constexpr __ph<1> _1{};
2024+
_LIBCPP_INLINE_VAR constexpr __ph<2> _2{};
2025+
_LIBCPP_INLINE_VAR constexpr __ph<3> _3{};
2026+
_LIBCPP_INLINE_VAR constexpr __ph<4> _4{};
2027+
_LIBCPP_INLINE_VAR constexpr __ph<5> _5{};
2028+
_LIBCPP_INLINE_VAR constexpr __ph<6> _6{};
2029+
_LIBCPP_INLINE_VAR constexpr __ph<7> _7{};
2030+
_LIBCPP_INLINE_VAR constexpr __ph<8> _8{};
2031+
_LIBCPP_INLINE_VAR constexpr __ph<9> _9{};
2032+
_LIBCPP_INLINE_VAR constexpr __ph<10> _10{};
20332033
#endif // defined(_LIBCPP_CXX03_LANG) || defined(_LIBCPP_BUILDING_BIND)
20342034

20352035
} // placeholders

libcxx/include/memory

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace std
1818
{
1919
2020
struct allocator_arg_t { };
21-
constexpr allocator_arg_t allocator_arg = allocator_arg_t();
21+
inline constexpr allocator_arg_t allocator_arg = allocator_arg_t();
2222
2323
template <class T, class Alloc> struct uses_allocator;
2424
@@ -631,6 +631,9 @@ template <class T> struct hash;
631631
template <class T, class D> struct hash<unique_ptr<T, D> >;
632632
template <class T> struct hash<shared_ptr<T> >;
633633
634+
template <class T, class Alloc>
635+
inline constexpr bool uses_allocator_v = uses_allocator<T, Alloc>::value;
636+
634637
// Pointer safety
635638
enum class pointer_safety { relaxed, preferred, strict };
636639
void declare_reachable(void *p);

libcxx/include/mutex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ struct defer_lock_t {};
9191
struct try_to_lock_t {};
9292
struct adopt_lock_t {};
9393
94-
constexpr defer_lock_t defer_lock{};
95-
constexpr try_to_lock_t try_to_lock{};
96-
constexpr adopt_lock_t adopt_lock{};
94+
inline constexpr defer_lock_t defer_lock{};
95+
inline constexpr try_to_lock_t try_to_lock{};
96+
inline constexpr adopt_lock_t adopt_lock{};
9797
9898
template <class Mutex>
9999
class lock_guard

libcxx/include/optional

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace std {
2222
2323
// 23.6.4, no-value state indicator
2424
struct nullopt_t{see below };
25-
constexpr nullopt_t nullopt(unspecified );
25+
inline constexpr nullopt_t nullopt(unspecified );
2626
2727
// 23.6.5, class bad_optional_access
2828
class bad_optional_access;
@@ -195,7 +195,7 @@ struct nullopt_t
195195
_LIBCPP_INLINE_VISIBILITY constexpr explicit nullopt_t(__secret_tag, __secret_tag) noexcept {}
196196
};
197197

198-
/* inline */ constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
198+
_LIBCPP_INLINE_VAR constexpr nullopt_t nullopt{nullopt_t::__secret_tag{}, nullopt_t::__secret_tag{}};
199199

200200
template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
201201
struct __optional_destruct_base;

libcxx/include/ratio

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,17 @@ typedef ratio< 1000000000000000000000, 1> zetta; // not supported
6363
typedef ratio<1000000000000000000000000, 1> yotta; // not supported
6464
6565
// 20.11.5, ratio comparison
66-
template <class R1, class R2> constexpr bool ratio_equal_v
66+
template <class R1, class R2> inline constexpr bool ratio_equal_v
6767
= ratio_equal<R1, R2>::value; // C++17
68-
template <class R1, class R2> constexpr bool ratio_not_equal_v
68+
template <class R1, class R2> inline constexpr bool ratio_not_equal_v
6969
= ratio_not_equal<R1, R2>::value; // C++17
70-
template <class R1, class R2> constexpr bool ratio_less_v
70+
template <class R1, class R2> inline constexpr bool ratio_less_v
7171
= ratio_less<R1, R2>::value; // C++17
72-
template <class R1, class R2> constexpr bool ratio_less_equal_v
72+
template <class R1, class R2> inline constexpr bool ratio_less_equal_v
7373
= ratio_less_equal<R1, R2>::value; // C++17
74-
template <class R1, class R2> constexpr bool ratio_greater_v
74+
template <class R1, class R2> inline constexpr bool ratio_greater_v
7575
= ratio_greater<R1, R2>::value; // C++17
76-
template <class R1, class R2> constexpr bool ratio_greater_equal_v
76+
template <class R1, class R2> inline constexpr bool ratio_greater_equal_v
7777
= ratio_greater_equal<R1, R2>::value; // C++17
7878
}
7979
*/
@@ -501,22 +501,28 @@ struct __ratio_gcd
501501
};
502502

503503
#if _LIBCPP_STD_VER > 14 && !defined(_LIBCPP_HAS_NO_VARIABLE_TEMPLATES)
504-
template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_equal_v
504+
template <class _R1, class _R2>
505+
_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_equal_v
505506
= ratio_equal<_R1, _R2>::value;
506507

507-
template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_not_equal_v
508+
template <class _R1, class _R2>
509+
_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_not_equal_v
508510
= ratio_not_equal<_R1, _R2>::value;
509511

510-
template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_less_v
512+
template <class _R1, class _R2>
513+
_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_less_v
511514
= ratio_less<_R1, _R2>::value;
512515

513-
template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_less_equal_v
516+
template <class _R1, class _R2>
517+
_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_less_equal_v
514518
= ratio_less_equal<_R1, _R2>::value;
515519

516-
template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_greater_v
520+
template <class _R1, class _R2>
521+
_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_greater_v
517522
= ratio_greater<_R1, _R2>::value;
518523

519-
template <class _R1, class _R2> _LIBCPP_CONSTEXPR bool ratio_greater_equal_v
524+
template <class _R1, class _R2>
525+
_LIBCPP_INLINE_VAR _LIBCPP_CONSTEXPR bool ratio_greater_equal_v
520526
= ratio_greater_equal<_R1, _R2>::value;
521527
#endif
522528

libcxx/include/system_error

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ template <class T> struct is_error_condition_enum
4747
: public false_type {};
4848
4949
template <class _Tp>
50-
constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
50+
inline constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
5151
5252
template <class _Tp>
53-
constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
53+
inline constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
5454
5555
class error_code
5656
{
@@ -246,7 +246,7 @@ struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
246246

247247
#if _LIBCPP_STD_VER > 14
248248
template <class _Tp>
249-
constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value;
249+
_LIBCPP_INLINE_VAR constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value;
250250
#endif
251251

252252
// is_error_condition_enum
@@ -257,7 +257,7 @@ struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
257257

258258
#if _LIBCPP_STD_VER > 14
259259
template <class _Tp>
260-
constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
260+
_LIBCPP_INLINE_VAR constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
261261
#endif
262262

263263
// Some error codes are not present on all platforms, so we provide equivalents

0 commit comments

Comments
 (0)