-
Notifications
You must be signed in to change notification settings - Fork 12k
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
[libc++] Make variables in templates inline #115785
Conversation
1c61450
to
86f3e34
Compare
@llvm/pr-subscribers-libcxxabi @llvm/pr-subscribers-libcxx Author: Nikolas Klauser (philnik777) ChangesPatch is 30.63 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/115785.diff 14 Files Affected:
diff --git a/libcxx/include/__random/discard_block_engine.h b/libcxx/include/__random/discard_block_engine.h
index f319557a573657..45951245a53401 100644
--- a/libcxx/include/__random/discard_block_engine.h
+++ b/libcxx/include/__random/discard_block_engine.h
@@ -43,8 +43,8 @@ class _LIBCPP_TEMPLATE_VIS discard_block_engine {
typedef typename _Engine::result_type result_type;
// engine characteristics
- static _LIBCPP_CONSTEXPR const size_t block_size = __p;
- static _LIBCPP_CONSTEXPR const size_t used_block = __r;
+ static inline _LIBCPP_CONSTEXPR const size_t block_size = __p;
+ static inline _LIBCPP_CONSTEXPR const size_t used_block = __r;
#ifdef _LIBCPP_CXX03_LANG
static const result_type _Min = _Engine::_Min;
@@ -110,12 +110,6 @@ class _LIBCPP_TEMPLATE_VIS discard_block_engine {
operator>>(basic_istream<_CharT, _Traits>& __is, discard_block_engine<_Eng, _Pp, _Rp>& __x);
};
-template <class _Engine, size_t __p, size_t __r>
-_LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::block_size;
-
-template <class _Engine, size_t __p, size_t __r>
-_LIBCPP_CONSTEXPR const size_t discard_block_engine<_Engine, __p, __r>::used_block;
-
template <class _Engine, size_t __p, size_t __r>
typename discard_block_engine<_Engine, __p, __r>::result_type discard_block_engine<_Engine, __p, __r>::operator()() {
if (__n_ >= static_cast<int>(__r)) {
diff --git a/libcxx/include/__random/linear_congruential_engine.h b/libcxx/include/__random/linear_congruential_engine.h
index a0afda4945cdcf..a6e63839d3fc16 100644
--- a/libcxx/include/__random/linear_congruential_engine.h
+++ b/libcxx/include/__random/linear_congruential_engine.h
@@ -251,12 +251,12 @@ class _LIBCPP_TEMPLATE_VIS linear_congruential_engine {
static_assert(_Min < _Max, "linear_congruential_engine invalid parameters");
// engine characteristics
- static _LIBCPP_CONSTEXPR const result_type multiplier = __a;
- static _LIBCPP_CONSTEXPR const result_type increment = __c;
- static _LIBCPP_CONSTEXPR const result_type modulus = __m;
+ static inline _LIBCPP_CONSTEXPR const result_type multiplier = __a;
+ static inline _LIBCPP_CONSTEXPR const result_type increment = __c;
+ static inline _LIBCPP_CONSTEXPR const result_type modulus = __m;
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
- static _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
+ static inline _LIBCPP_CONSTEXPR const result_type default_seed = 1u;
// constructors and seeding functions
#ifndef _LIBCPP_CXX03_LANG
@@ -318,22 +318,6 @@ class _LIBCPP_TEMPLATE_VIS linear_congruential_engine {
operator>>(basic_istream<_CharT, _Traits>& __is, linear_congruential_engine<_Up, _Ap, _Cp, _Np>& __x);
};
-template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
-_LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
- linear_congruential_engine<_UIntType, __a, __c, __m>::multiplier;
-
-template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
-_LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
- linear_congruential_engine<_UIntType, __a, __c, __m>::increment;
-
-template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
-_LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
- linear_congruential_engine<_UIntType, __a, __c, __m>::modulus;
-
-template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
-_LIBCPP_CONSTEXPR const typename linear_congruential_engine<_UIntType, __a, __c, __m>::result_type
- linear_congruential_engine<_UIntType, __a, __c, __m>::default_seed;
-
template <class _UIntType, _UIntType __a, _UIntType __c, _UIntType __m>
template <class _Sseq>
void linear_congruential_engine<_UIntType, __a, __c, __m>::__seed(_Sseq& __q, integral_constant<unsigned, 1>) {
diff --git a/libcxx/include/__random/mersenne_twister_engine.h b/libcxx/include/__random/mersenne_twister_engine.h
index 9dd87f9ce71a11..a23feffff0c89c 100644
--- a/libcxx/include/__random/mersenne_twister_engine.h
+++ b/libcxx/include/__random/mersenne_twister_engine.h
@@ -166,22 +166,22 @@ class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine {
static_assert(__f <= _Max, "mersenne_twister_engine invalid parameters");
// engine characteristics
- static _LIBCPP_CONSTEXPR const size_t word_size = __w;
- static _LIBCPP_CONSTEXPR const size_t state_size = __n;
- static _LIBCPP_CONSTEXPR const size_t shift_size = __m;
- static _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
- static _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
- static _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
- static _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
- static _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
- static _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
- static _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
- static _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
- static _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
- static _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
+ static inline _LIBCPP_CONSTEXPR const size_t word_size = __w;
+ static inline _LIBCPP_CONSTEXPR const size_t state_size = __n;
+ static inline _LIBCPP_CONSTEXPR const size_t shift_size = __m;
+ static inline _LIBCPP_CONSTEXPR const size_t mask_bits = __r;
+ static inline _LIBCPP_CONSTEXPR const result_type xor_mask = __a;
+ static inline _LIBCPP_CONSTEXPR const size_t tempering_u = __u;
+ static inline _LIBCPP_CONSTEXPR const result_type tempering_d = __d;
+ static inline _LIBCPP_CONSTEXPR const size_t tempering_s = __s;
+ static inline _LIBCPP_CONSTEXPR const result_type tempering_b = __b;
+ static inline _LIBCPP_CONSTEXPR const size_t tempering_t = __t;
+ static inline _LIBCPP_CONSTEXPR const result_type tempering_c = __c;
+ static inline _LIBCPP_CONSTEXPR const size_t tempering_l = __l;
+ static inline _LIBCPP_CONSTEXPR const result_type initialization_multiplier = __f;
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
- static _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
+ static inline _LIBCPP_CONSTEXPR const result_type default_seed = 5489u;
// constructors and seeding functions
#ifndef _LIBCPP_CXX03_LANG
@@ -310,329 +310,6 @@ class _LIBCPP_TEMPLATE_VIS mersenne_twister_engine {
}
};
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const size_t
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::word_size;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const size_t
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::state_size;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const size_t
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::shift_size;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const size_t
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::mask_bits;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
- _UIntType,
- __w,
- __n,
- __m,
- __r,
- __a,
- __u,
- __d,
- __s,
- __b,
- __t,
- __c,
- __l,
- __f>::result_type
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::xor_mask;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const size_t
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_u;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
- _UIntType,
- __w,
- __n,
- __m,
- __r,
- __a,
- __u,
- __d,
- __s,
- __b,
- __t,
- __c,
- __l,
- __f>::result_type
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_d;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const size_t
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_s;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
- _UIntType,
- __w,
- __n,
- __m,
- __r,
- __a,
- __u,
- __d,
- __s,
- __b,
- __t,
- __c,
- __l,
- __f>::result_type
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_b;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const size_t
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_t;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
- _UIntType,
- __w,
- __n,
- __m,
- __r,
- __a,
- __u,
- __d,
- __s,
- __b,
- __t,
- __c,
- __l,
- __f>::result_type
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_c;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const size_t
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::tempering_l;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
- _UIntType,
- __w,
- __n,
- __m,
- __r,
- __a,
- __u,
- __d,
- __s,
- __b,
- __t,
- __c,
- __l,
- __f>::result_type
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::
- initialization_multiplier;
-
-template <class _UIntType,
- size_t __w,
- size_t __n,
- size_t __m,
- size_t __r,
- _UIntType __a,
- size_t __u,
- _UIntType __d,
- size_t __s,
- _UIntType __b,
- size_t __t,
- _UIntType __c,
- size_t __l,
- _UIntType __f>
-_LIBCPP_CONSTEXPR const typename mersenne_twister_engine<
- _UIntType,
- __w,
- __n,
- __m,
- __r,
- __a,
- __u,
- __d,
- __s,
- __b,
- __t,
- __c,
- __l,
- __f>::result_type
- mersenne_twister_engine<_UIntType, __w, __n, __m, __r, __a, __u, __d, __s, __b, __t, __c, __l, __f>::default_seed;
-
template <class _UIntType,
size_t __w,
size_t __n,
diff --git a/libcxx/include/__random/shuffle_order_engine.h b/libcxx/include/__random/shuffle_order_engine.h
index 53f6c08971105e..11a46689a0fa67 100644
--- a/libcxx/include/__random/shuffle_order_engine.h
+++ b/libcxx/include/__random/shuffle_order_engine.h
@@ -66,7 +66,7 @@ class _LIBCPP_TEMPLATE_VIS shuffle_order_engine {
public:
// engine characteristics
- static _LIBCPP_CONSTEXPR const size_t table_size = __k;
+ static inline _LIBCPP_CONSTEXPR const size_t table_size = __k;
#ifdef _LIBCPP_CXX03_LANG
static const result_type _Min = _Engine::_Min;
@@ -173,9 +173,6 @@ class _LIBCPP_TEMPLATE_VIS shuffle_order_engine {
}
};
-template <class _Engine, size_t __k>
-_LIBCPP_CONSTEXPR const size_t shuffle_order_engine<_Engine, __k>::table_size;
-
template <class _Eng, size_t _Kp>
_LIBCPP_HIDE_FROM_ABI bool
operator==(const shuffle_order_engine<_Eng, _Kp>& __x, const shuffle_order_engine<_Eng, _Kp>& __y) {
diff --git a/libcxx/include/__random/subtract_with_carry_engine.h b/libcxx/include/__random/subtract_with_carry_engine.h
index e087ab4a3c2c7b..40dfaf4016ada0 100644
--- a/libcxx/include/__random/subtract_with_carry_engine.h
+++ b/libcxx/include/__random/subtract_with_carry_engine.h
@@ -72,12 +72,12 @@ class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine {
static_assert(_Min < _Max, "subtract_with_carry_engine invalid parameters");
// engine characteristics
- static _LIBCPP_CONSTEXPR const size_t word_size = __w;
- static _LIBCPP_CONSTEXPR const size_t short_lag = __s;
- static _LIBCPP_CONSTEXPR const size_t long_lag = __r;
+ static inline _LIBCPP_CONSTEXPR const size_t word_size = __w;
+ static inline _LIBCPP_CONSTEXPR const size_t short_lag = __s;
+ static inline _LIBCPP_CONSTEXPR const size_t long_lag = __r;
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type min() { return _Min; }
_LIBCPP_HIDE_FROM_ABI static _LIBCPP_CONSTEXPR result_type max() { return _Max; }
- static _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
+ static inline _LIBCPP_CONSTEXPR const result_type default_seed = 19780503u;
// constructors and seeding functions
#ifndef _LIBCPP_CXX03_LANG
@@ -130,19 +130,6 @@ class _LIBCPP_TEMPLATE_VIS subtract_with_carry_engine {
_LIBCPP_HIDE_FROM_ABI void __seed(_Sseq& __q, integral_constant<unsigned, 2>);
};
-template <class _UIntType, size_t __w, size_t __s, size_t __r>
-_LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::word_size;
-
-template <class _UIntType, size_t __w, size_t __s, size_t __r>
-_LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::short_lag;
-
-template <class _UIntType, size_t __w, size_t __s, size_t __r>
-_LIBCPP_CONSTEXPR const size_t subtract_with_carry_engine<_UIntType, __w, __s, __r>::long_lag;
-
-template <class _UIntType, size_t __w, size_t __s, size_t __r>
-_LIBCPP_CONSTEXPR const typename subtract_with_carry_engine<_UIntType, __w, __s, __r>::result_type
- subtract_with_carry_engine<_UIntType, __w, __s, __r>::default_seed;
-
template <class _UIntType, size_t __w, size_t __s, size_t __r>
void subtract_with_carry_engine<_UIntType, __w, __s, __r>::seed(result_type __sd, integral_constant<unsigned, 1>) {
linear_congruential_engine<result_type, 40014u, 0u, 2147483563u> __e(__sd == 0u ? default_seed : __sd);
diff --git a/libcxx/include/__type_traits/integral_constant.h b/libcxx/include/__type_traits/integral_constant.h
index 23e87e27feff55..b8c75c546aa942 100644
--- a/libcxx/include/__type_traits/integral_constant.h
+++ b/libcxx/include/__type_traits/integral_constant.h
@@ -19,7 +19,7 @@ _LIBCPP_BEGIN_NAMESPACE_STD
template <class _Tp, _Tp __v>
struct _LIBCPP_TEMPLATE_VIS integral_constant {
- static _LIBCPP_CONSTEXPR const _Tp value = __v;
+ static inline _LIBCPP_CONSTEXPR const _Tp value = __v;
typedef _Tp value_type;
typedef integral_constant type;
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR operator value_type() const _NOEXCEPT { return value; }
@@ -28,9 +28,6 @@ struct _LIBCPP_TEMPLATE_VIS integral_constant {
#endif
};
-template <class _Tp, _Tp __v>
-_LIBCPP_CONSTEXPR const _Tp integral_constant<_Tp, __v>::value;
-
typedef integral_constant<bool, true> true_type;
typedef integral_constant<bool, false> false_type;
diff --git a/libcxx/include/any b/libcxx/include/any
index 719dc2cf999e50..cae56fa376e057 100644
--- a/libcxx/include/any
+++ b/libcxx/include/any
@@ -166,8 +166,6 @@ template <class _Tp>
struct _LIBCPP_TEMPLATE_VIS __unique_typeinfo {
static constexpr int __id = 0;
};
-template <class _Tp>
-constexpr int __unique_typeinfo<_Tp>::__id;
template <class _Tp>
inline _LIBCPP_HIDE_FROM_ABI constexpr const void* __get_fallback_typeid() {
diff --git a/libcxx/include/limits b/libcxx/include/limits
index b85c66257d27b7..da0a92d7daff4a 100644
--- a/libcxx/include/limits
+++ b/libcxx/include/limits
@@ -464,18 +464,18 @@ class _LIBCPP_TEMPLATE_VIS numeric_limits : private __libcpp_numeric_limits<_Tp>
typedef typename __base::type type;
public:
- static _LIBCPP_CONSTEXPR const bool is_specialized = __base::is_specialized;
+ static inline _LIBCPP_CONSTEXPR const bool is_specialized = __base::is_specialized;
[[__nodiscard__]] _LI...
[truncated]
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide a bit more context around this change? These variables are not marked inline
in the spec AFAICT. Is this just to allow defining them inside the classes and avoiding a bunch of verbose declarations?
The variables are all |
That is the one thing I forget the most in all of C++. Every time I come across it, I fail to remember that LGTM. |
86f3e34
to
482eadf
Compare
The variables are all
constexpr
, which impliesinline
. Since they aren'tconstexpr
in C++03 they're also notinline
there. Because of that we define them out-of-line currently. Instead we can use the C++17 extension ofinline
variables, which results in the same weak definitions of the variables but without having all the boilerplate.