ofats / any_invocable

Сonservative, move-only equivalent of std::function
MIT License
20 stars 3 forks source link
cplusplus cplusplus-17 cplusplus-20 cpp cpp17 cpp20 std

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>()] { /*...*/ };