00001 // Copyright 2017 The Abseil Authors. 00002 // 00003 // Licensed under the Apache License, Version 2.0 (the "License"); 00004 // you may not use this file except in compliance with the License. 00005 // You may obtain a copy of the License at 00006 // 00007 // https://www.apache.org/licenses/LICENSE-2.0 00008 // 00009 // Unless required by applicable law or agreed to in writing, software 00010 // distributed under the License is distributed on an "AS IS" BASIS, 00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00012 // See the License for the specific language governing permissions and 00013 // limitations under the License. 00014 00015 #ifndef ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_ 00016 #define ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_ 00017 00018 #include <type_traits> 00019 00020 #include "absl/base/internal/identity.h" 00021 00022 // File: 00023 // This file define a macro that allows the creation of or emulation of C++17 00024 // inline variables based on whether or not the feature is supported. 00025 00027 // Macro: ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init) 00028 // 00029 // Description: 00030 // Expands to the equivalent of an inline constexpr instance of the specified 00031 // `type` and `name`, initialized to the value `init`. If the compiler being 00032 // used is detected as supporting actual inline variables as a language 00033 // feature, then the macro expands to an actual inline variable definition. 00034 // 00035 // Requires: 00036 // `type` is a type that is usable in an extern variable declaration. 00037 // 00038 // Requires: `name` is a valid identifier 00039 // 00040 // Requires: 00041 // `init` is an expression that can be used in the following definition: 00042 // constexpr type name = init; 00043 // 00044 // Usage: 00045 // 00046 // // Equivalent to: `inline constexpr size_t variant_npos = -1;` 00047 // ABSL_INTERNAL_INLINE_CONSTEXPR(size_t, variant_npos, -1); 00048 // 00049 // Differences in implementation: 00050 // For a direct, language-level inline variable, decltype(name) will be the 00051 // type that was specified along with const qualification, whereas for 00052 // emulated inline variables, decltype(name) may be different (in practice 00053 // it will likely be a reference type). 00055 00056 #ifdef __cpp_inline_variables 00057 00058 // Clang's -Wmissing-variable-declarations option erroneously warned that 00059 // inline constexpr objects need to be pre-declared. This has now been fixed, 00060 // but we will need to support this workaround for people building with older 00061 // versions of clang. 00062 // 00063 // Bug: https://bugs.llvm.org/show_bug.cgi?id=35862 00064 // 00065 // Note: 00066 // identity_t is used here so that the const and name are in the 00067 // appropriate place for pointer types, reference types, function pointer 00068 // types, etc.. 00069 #if defined(__clang__) 00070 #define ABSL_INTERNAL_EXTERN_DECL(type, name) \ 00071 extern const ::absl::internal::identity_t<type> name; 00072 #else // Otherwise, just define the macro to do nothing. 00073 #define ABSL_INTERNAL_EXTERN_DECL(type, name) 00074 #endif // defined(__clang__) 00075 00076 // See above comment at top of file for details. 00077 #define ABSL_INTERNAL_INLINE_CONSTEXPR(type, name, init) \ 00078 ABSL_INTERNAL_EXTERN_DECL(type, name) \ 00079 inline constexpr ::absl::internal::identity_t<type> name = init 00080 00081 #else 00082 00083 // See above comment at top of file for details. 00084 // 00085 // Note: 00086 // identity_t is used here so that the const and name are in the 00087 // appropriate place for pointer types, reference types, function pointer 00088 // types, etc.. 00089 #define ABSL_INTERNAL_INLINE_CONSTEXPR(var_type, name, init) \ 00090 template <class /*AbslInternalDummy*/ = void> \ 00091 struct AbslInternalInlineVariableHolder##name { \ 00092 static constexpr ::absl::internal::identity_t<var_type> kInstance = init; \ 00093 }; \ 00094 \ 00095 template <class AbslInternalDummy> \ 00096 constexpr ::absl::internal::identity_t<var_type> \ 00097 AbslInternalInlineVariableHolder##name<AbslInternalDummy>::kInstance; \ 00098 \ 00099 static constexpr const ::absl::internal::identity_t<var_type>& \ 00100 name = /* NOLINT */ \ 00101 AbslInternalInlineVariableHolder##name<>::kInstance; \ 00102 static_assert(sizeof(void (*)(decltype(name))) != 0, \ 00103 "Silence unused variable warnings.") 00104 00105 #endif // __cpp_inline_variables 00106 00107 #endif // ABSL_BASE_INTERNAL_INLINE_VARIABLE_EMULATION_H_