mirror of
https://github.com/SerenityOS/serenity.git
synced 2026-11-08 17:49:04 -10:00
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`.
15 lines
268 B
C++
15 lines
268 B
C++
/*
|
|
* 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);
|