forked from SerenityOS/serenity
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AK: Make Function a little bit constexpr
Just enough to make it possible to pass a lambda to a constexpr function taking an AK::Function parameter and have that constexpr function call the passed-in AK::Function. The main thing is that bit_cast<>s of pointers aren't ok in constexpr functions, and neither is placement new. So add a union with stronger typing for the constexpr case. The body of the `if (is_constexpr_evaluated())` in `init_with_callable()` is identical to the `if constexpr` right after it. But `if constexpr (is_constexpr_evaluated())` always evaluates `is_constexpr_evaluated()` in a constexpr context, so this can't just add ` || is_constexpr_evaluated()` to that `if constexpr`.
- Loading branch information
Showing
4 changed files
with
55 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright (c) 2024, Nico Weber <[email protected]> | ||
* | ||
* SPDX-License-Identifier: BSD-2-Clause | ||
*/ | ||
|
||
#include <AK/Function.h> | ||
|
||
constexpr int const_call(Function<int(int)> f, int i) | ||
{ | ||
return f(i); | ||
} | ||
|
||
constinit int i = const_call([](int i) { return i; }, 4); |