1*9356374aSAndroid Build Coastguard Worker // Copyright 2017 The Abseil Authors. 2*9356374aSAndroid Build Coastguard Worker // 3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License"); 4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License. 5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at 6*9356374aSAndroid Build Coastguard Worker // 7*9356374aSAndroid Build Coastguard Worker // https://www.apache.org/licenses/LICENSE-2.0 8*9356374aSAndroid Build Coastguard Worker // 9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software 10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS, 11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and 13*9356374aSAndroid Build Coastguard Worker // limitations under the License. 14*9356374aSAndroid Build Coastguard Worker 15*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_BASE_INTERNAL_INLINE_VARIABLE_H_ 16*9356374aSAndroid Build Coastguard Worker #define ABSL_BASE_INTERNAL_INLINE_VARIABLE_H_ 17*9356374aSAndroid Build Coastguard Worker 18*9356374aSAndroid Build Coastguard Worker #include <type_traits> 19*9356374aSAndroid Build Coastguard Worker 20*9356374aSAndroid Build Coastguard Worker #include "absl/base/internal/identity.h" 21*9356374aSAndroid Build Coastguard Worker 22*9356374aSAndroid Build Coastguard Worker // File: 23*9356374aSAndroid Build Coastguard Worker // This file define a macro that allows the creation of or emulation of C++17 24*9356374aSAndroid Build Coastguard Worker // inline variables based on whether or not the feature is supported. 25*9356374aSAndroid Build Coastguard Worker 26*9356374aSAndroid Build Coastguard Worker //////////////////////////////////////////////////////////////////////////////// 27*9356374aSAndroid Build Coastguard Worker // Macro: ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init) 28*9356374aSAndroid Build Coastguard Worker // 29*9356374aSAndroid Build Coastguard Worker // Description: 30*9356374aSAndroid Build Coastguard Worker // Expands to the equivalent of an inline constexpr instance of the specified 31*9356374aSAndroid Build Coastguard Worker // `type` and `name`, initialized to the value `init`. If the compiler being 32*9356374aSAndroid Build Coastguard Worker // used is detected as supporting actual inline variables as a language 33*9356374aSAndroid Build Coastguard Worker // feature, then the macro expands to an actual inline variable definition. 34*9356374aSAndroid Build Coastguard Worker // 35*9356374aSAndroid Build Coastguard Worker // Requires: 36*9356374aSAndroid Build Coastguard Worker // `type` is a type that is usable in an extern variable declaration. 37*9356374aSAndroid Build Coastguard Worker // 38*9356374aSAndroid Build Coastguard Worker // Requires: `name` is a valid identifier 39*9356374aSAndroid Build Coastguard Worker // 40*9356374aSAndroid Build Coastguard Worker // Requires: 41*9356374aSAndroid Build Coastguard Worker // `init` is an expression that can be used in the following definition: 42*9356374aSAndroid Build Coastguard Worker // constexpr type name = init; 43*9356374aSAndroid Build Coastguard Worker // 44*9356374aSAndroid Build Coastguard Worker // Usage: 45*9356374aSAndroid Build Coastguard Worker // 46*9356374aSAndroid Build Coastguard Worker // // Equivalent to: `inline constexpr size_t variant_npos = -1;` 47*9356374aSAndroid Build Coastguard Worker // ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, -1); 48*9356374aSAndroid Build Coastguard Worker // 49*9356374aSAndroid Build Coastguard Worker // Differences in implementation: 50*9356374aSAndroid Build Coastguard Worker // For a direct, language-level inline variable, decltype(name) will be the 51*9356374aSAndroid Build Coastguard Worker // type that was specified along with const qualification, whereas for 52*9356374aSAndroid Build Coastguard Worker // emulated inline variables, decltype(name) may be different (in practice 53*9356374aSAndroid Build Coastguard Worker // it will likely be a reference type). 54*9356374aSAndroid Build Coastguard Worker //////////////////////////////////////////////////////////////////////////////// 55*9356374aSAndroid Build Coastguard Worker 56*9356374aSAndroid Build Coastguard Worker #ifdef __cpp_inline_variables 57*9356374aSAndroid Build Coastguard Worker 58*9356374aSAndroid Build Coastguard Worker // Clang's -Wmissing-variable-declarations option erroneously warned that 59*9356374aSAndroid Build Coastguard Worker // inline constexpr objects need to be pre-declared. This has now been fixed, 60*9356374aSAndroid Build Coastguard Worker // but we will need to support this workaround for people building with older 61*9356374aSAndroid Build Coastguard Worker // versions of clang. 62*9356374aSAndroid Build Coastguard Worker // 63*9356374aSAndroid Build Coastguard Worker // Bug: https://bugs.llvm.org/show_bug.cgi?id=35862 64*9356374aSAndroid Build Coastguard Worker // 65*9356374aSAndroid Build Coastguard Worker // Note: 66*9356374aSAndroid Build Coastguard Worker // type_identity_t is used here so that the const and name are in the 67*9356374aSAndroid Build Coastguard Worker // appropriate place for pointer types, reference types, function pointer 68*9356374aSAndroid Build Coastguard Worker // types, etc.. 69*9356374aSAndroid Build Coastguard Worker #if defined(__clang__) 70*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_EXTERN_DECL(type, name) \ 71*9356374aSAndroid Build Coastguard Worker extern const ::absl::internal::type_identity_t<type> name; 72*9356374aSAndroid Build Coastguard Worker #else // Otherwise, just define the macro to do nothing. 73*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_EXTERN_DECL(type, name) 74*9356374aSAndroid Build Coastguard Worker #endif // defined(__clang__) 75*9356374aSAndroid Build Coastguard Worker 76*9356374aSAndroid Build Coastguard Worker // See above comment at top of file for details. 77*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init) \ 78*9356374aSAndroid Build Coastguard Worker ABSL_INTERNAL_EXTERN_DECL(type, name) \ 79*9356374aSAndroid Build Coastguard Worker inline constexpr ::absl::internal::type_identity_t<type> name = init 80*9356374aSAndroid Build Coastguard Worker 81*9356374aSAndroid Build Coastguard Worker #else 82*9356374aSAndroid Build Coastguard Worker 83*9356374aSAndroid Build Coastguard Worker // See above comment at top of file for details. 84*9356374aSAndroid Build Coastguard Worker // 85*9356374aSAndroid Build Coastguard Worker // Note: 86*9356374aSAndroid Build Coastguard Worker // type_identity_t is used here so that the const and name are in the 87*9356374aSAndroid Build Coastguard Worker // appropriate place for pointer types, reference types, function pointer 88*9356374aSAndroid Build Coastguard Worker // types, etc.. 89*9356374aSAndroid Build Coastguard Worker #define ABSL_INTERNAL_INLINE_CONSTEXPR(var_type, name, init) \ 90*9356374aSAndroid Build Coastguard Worker template <class /*AbslInternalDummy*/ = void> \ 91*9356374aSAndroid Build Coastguard Worker struct AbslInternalInlineVariableHolder##name { \ 92*9356374aSAndroid Build Coastguard Worker static constexpr ::absl::internal::type_identity_t<var_type> kInstance = \ 93*9356374aSAndroid Build Coastguard Worker init; \ 94*9356374aSAndroid Build Coastguard Worker }; \ 95*9356374aSAndroid Build Coastguard Worker \ 96*9356374aSAndroid Build Coastguard Worker template <class AbslInternalDummy> \ 97*9356374aSAndroid Build Coastguard Worker constexpr ::absl::internal::type_identity_t<var_type> \ 98*9356374aSAndroid Build Coastguard Worker AbslInternalInlineVariableHolder##name<AbslInternalDummy>::kInstance; \ 99*9356374aSAndroid Build Coastguard Worker \ 100*9356374aSAndroid Build Coastguard Worker static constexpr const ::absl::internal::type_identity_t<var_type>& \ 101*9356374aSAndroid Build Coastguard Worker name = /* NOLINT */ \ 102*9356374aSAndroid Build Coastguard Worker AbslInternalInlineVariableHolder##name<>::kInstance; \ 103*9356374aSAndroid Build Coastguard Worker static_assert(sizeof(void (*)(decltype(name))) != 0, \ 104*9356374aSAndroid Build Coastguard Worker "Silence unused variable warnings.") 105*9356374aSAndroid Build Coastguard Worker 106*9356374aSAndroid Build Coastguard Worker #endif // __cpp_inline_variables 107*9356374aSAndroid Build Coastguard Worker 108*9356374aSAndroid Build Coastguard Worker #endif // ABSL_BASE_INTERNAL_INLINE_VARIABLE_H_ 109