Skip to content

Сonservative, move-only equivalent of std::function

License

Notifications You must be signed in to change notification settings

ofats/any_invocable

Repository files navigation

any_invocable

One of possible implementations of std::any_invocable proposed in P0288R5.

This paper proposes a conservative, move-only equivalent of std::function.

Current build

Linux build status macOS build status Windows build status

Motivating Example

Such a code:

// requires: C++14
#include <functional>
#include <memory>

struct widget {
    // ...
};

int main() {
    std::function<void()> f = [w_ptr = std::make_unique<widget>()] { /*...*/ };
}

Will result in:

error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = widget; _Dp = std::default_delete<widget>]

In other words, we can't store move-only callables in std::function.

At the same time such a code will work:

std::any_invocable<void()> f = [w_ptr = std::make_unique<widget>()] { /*...*/ };