Skip to content

Commit 591e60b

Browse files
[ADT] Use "inline static" to initialize CallbacksHolder (NFC) (#160003)
In C++17, we can initialize a static member variable with "inline static" as part of the class definition. With this, we can eliminate the out-of-line static initializers that are a bit hard to decipher.
1 parent cafc064 commit 591e60b

File tree

1 file changed

+3
-16
lines changed

1 file changed

+3
-16
lines changed

llvm/include/llvm/ADT/FunctionExtras.h

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,22 +231,22 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
231231

232232
// The pointers to call/move/destroy functions are determined for each
233233
// callable type (and called-as type, which determines the overload chosen).
234-
// (definitions are out-of-line).
235234

236235
// By default, we need an object that contains all the different
237236
// type erased behaviors needed. Create a static instance of the struct type
238237
// here and each instance will contain a pointer to it.
239238
// Wrap in a struct to avoid https://gcc.gnu.org/PR71954
240239
template <typename CallableT, typename CalledAs, typename Enable = void>
241240
struct CallbacksHolder {
242-
static NonTrivialCallbacks Callbacks;
241+
inline static NonTrivialCallbacks Callbacks = {
242+
&CallImpl<CalledAs>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
243243
};
244244
// See if we can create a trivial callback. We need the callable to be
245245
// trivially moved and trivially destroyed so that we don't have to store
246246
// type erased callbacks for those operations.
247247
template <typename CallableT, typename CalledAs>
248248
struct CallbacksHolder<CallableT, CalledAs, EnableIfTrivial<CallableT>> {
249-
static TrivialCallback Callbacks;
249+
inline static TrivialCallback Callbacks = {&CallImpl<CalledAs>};
250250
};
251251

252252
// A simple tag type so the call-as type to be passed to the constructor.
@@ -344,19 +344,6 @@ template <typename ReturnT, typename... ParamTs> class UniqueFunctionBase {
344344
}
345345
};
346346

347-
template <typename R, typename... P>
348-
template <typename CallableT, typename CalledAsT, typename Enable>
349-
typename UniqueFunctionBase<R, P...>::NonTrivialCallbacks UniqueFunctionBase<
350-
R, P...>::CallbacksHolder<CallableT, CalledAsT, Enable>::Callbacks = {
351-
&CallImpl<CalledAsT>, &MoveImpl<CallableT>, &DestroyImpl<CallableT>};
352-
353-
template <typename R, typename... P>
354-
template <typename CallableT, typename CalledAsT>
355-
typename UniqueFunctionBase<R, P...>::TrivialCallback
356-
UniqueFunctionBase<R, P...>::CallbacksHolder<
357-
CallableT, CalledAsT, EnableIfTrivial<CallableT>>::Callbacks{
358-
&CallImpl<CalledAsT>};
359-
360347
} // namespace detail
361348

362349
template <typename R, typename... P>

0 commit comments

Comments
 (0)