ebd::basic_fn is a basic function wrapper that users can customize. It is an alias of ebd::detail::function that allows direct specification of all configuration parameters, providing the most flexible way to instantiate a function wrapper.
| Parameter | Description |
|---|---|
Signature |
Function signature, e.g., Ret(Args...) or Ret(Args...) const. |
BufferSize |
Size of the internal storage (in bytes). The value will be automatically aligned to a multiple of Alignment. |
Alignment |
Alignment of the internal storage (in bytes). |
IsCopyable |
If true, the stored callable object must be copy-constructible; otherwise, move-only is sufficient. |
IsView |
If true, the wrapper acts as a non-owning view (no copy/move/destroy of the target). |
IsThrowing |
If true, calling an empty wrapper throws std::bad_function_call (if exceptions are enabled); otherwise, std::terminate is called. |
AssertObjectNoThrow |
If true, an owning wrapper additionally requires the callable object's copy and move constructors, when present, to be noexcept; a deleted move constructor is allowed, and a deleted copy constructor is allowed only when IsCopyable is false. A noexcept destructor is required regardless of this flag. View wrappers (IsView is true) ignore both requirements, because they never construct or destroy the target. |
All member functions of ebd::detail::function are available for ebd::basic_fn. For detailed documentation, see ebd::detail::function.
#include "embed/embed_function.hpp"
// Define a custom move-only, non-throwing function wrapper
template <
typename Signature,
std::size_t BufferSize = ebd::detail::default_values::owning::buffer_size,
std::size_t Alignment = ebd::detail::default_values::owning::alignment
>
using unique_safe_fn = ebd::basic_fn<
Signature,
BufferSize,
Alignment,
false, // IsCopyable (move-only)
false, // IsView (owning)
false, // IsThrowing (call std::terminate() when empty)
true // AssertObjectNoThrow (require noexcept operations)
>;
// Usage
unique_safe_fn<void(int)> fn;
// Assign a move-only, noexcept lambda
std::unique_ptr<int> value = std::make_unique<int>(42);
fn = [value = std::move(value)](int x) noexcept { /* use value */ };
fn(100);- Prefer using the predefined aliases (
ebd::fn,ebd::unique_fn,ebd::classic_fn,ebd::fn_ref) unless you need a combination not covered by them. - The buffer size is automatically aligned to a multiple of
Alignment. - If the callable object is too large or requires a larger alignment than the specified
BufferSize/Alignment, astatic_assertwill be triggered at compile time.
ebd::detail::function- The underlying implementationebd::fn- For copyable callablesebd::unique_fn- For move-only callablesebd::classic_fn- For callables withstd::bad_function_callon emptyebd::fn_ref- For non-owning views of callables