Coder Social home page Coder Social logo

ofats / any_invocable Goto Github PK

View Code? Open in Web Editor NEW
20.0 3.0 3.0 157 KB

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

License: MIT License

CMake 1.91% Shell 1.89% C++ 82.56% Starlark 13.64%
std cpp cpp17 cpp20 cplusplus cplusplus-17 cplusplus-20

any_invocable's Introduction

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

any_invocable's People

Contributors

ofats avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

any_invocable's Issues

const any_invocable seems to be uninvokable

Not sure if this is an issue with the implementation or I did something wrong.

Trying to invoke const ofats::any_invocable<void()> or const ofats::any_invocable<void() const> results in compilation errors. The former because matching call operator could not be found, the latter is because any_invocable_impl::call doesn't have a const variant. Please see an example on godbolt: https://godbolt.org/z/Wczddnsoh

The const qualifier comes from being a member of a class and being invoked in const function, like:

struct stuff
{
  void call() const { f(); }
  ofats::any_invocable<void()> f; // or `void() const`
}

std::vector incompatible?

The following fails to compile on macOS Clang but does compile on Linux (both GCC and Clang):

    std::vector<ofats::any_invocable<void()>> v;
    
    v.emplace_back([]() {
    
    });

You can test with GitHub Actions on macos-latest. This code should compile, right?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.