Files
serenity/Tests/AK/TestFunction.cpp
Nico Weber e4c36876d2 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`.
2024-12-20 12:05:06 -05:00

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);