template class EE::SmallUniqueFunction<R(Args…), InlineSize>

Overview

Move-only type-erased callable with inline storage and a heap fallback. More…

#include <small_unique_function.hpp>

template <typename R, typename... Args, std::size_t InlineSize>
class SmallUniqueFunction<R(Args...), InlineSize> {
public:
    // structs

    template <>
    struct SmallUniqueFunction<R(Args...), InlineSize>;

    // construction

    SmallUniqueFunction();
    SmallUniqueFunction(std::nullptr_t);
    SmallUniqueFunction(const SmallUniqueFunction&);
    SmallUniqueFunction(SmallUniqueFunction&& other);

    template <typename Callable, typename Decayed = std::decay_t<Callable>, std::enable_if_t<!std::is_same_v<Decayed, SmallUniqueFunction>&&std::is_invocable_r_v<R, Decayed&, Args...>, int> = 0>
    SmallUniqueFunction(Callable&& callable);
    ~SmallUniqueFunction();

    // methods

    SmallUniqueFunction& operator=(const SmallUniqueFunction&);
    SmallUniqueFunction& operator=(SmallUniqueFunction&& other);

    template <typename Callable, typename Decayed = std::decay_t<Callable>, std::enable_if_t<!std::is_same_v<Decayed, SmallUniqueFunction>&&std::is_invocable_r_v<R, Decayed&, Args...>, int> = 0>
    SmallUniqueFunction& operator=(Callable&& callable);

    SmallUniqueFunction& operator=(std::nullptr_t);
    operator bool() const;
    R operator()(Args... args);
    void reset();
    bool isInline() const;
    static constexpr std::size_t inlineSize();
};

Detailed Documentation

Move-only type-erased callable with inline storage and a heap fallback.

Unlike SmallFunction, the stored callable only needs to be move-constructible, which allows closures that own move-only state (tasks, unique pointers, file handles) to be queued on executors without wrapping them in shared heap holders. Callables larger than InlineSize, or whose alignment or move constructor make inline relocation unsafe, are stored on the heap.

The callable is invoked as a non-const lvalue: it may mutate its captures, and the object can be invoked more than once unless the stored callable itself consumes its state.

Methods

bool isInline() const

Returns:

True when the stored callable lives in the inline buffer.