GithubHelp home page GithubHelp logo

mguludag / invoker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from calebxyz/invoker

2.0 0.0 0.0 56 KB

Generic Invocation wrapper , lightweight function

License: Apache License 2.0

C++ 100.00%

invoker's Introduction

Invoker

Generic Invocation wrapper , lightweight function written in C++>=17

As you may know std::functions are heavy because they use Type Erasure and their invocation might be much slower then a regular lambdas or a free functions but on the other hand we cannot use lambdas in virtual functions (yet) because we need to make the function templated.

So this small wrapper does the trick for you, you may use it very similar to std::function, passing a certain type of function pointer or just use it as a auto deduced template type where you may do that.

As you can see the performance is the same as using a lambda and it is much better then with std::function, The benchmark was compiled with Clang 13.

Usage example:

void foo(int i){
};

struct functor{
    bool operator ()(){
        return true;
    }
};

int doit(Invoker<int(const int&)> f){
    return f(1);
}

int main(){
    Invoker<int(const int&)> f1([](const int& i) {return i;});
    auto f2 = Invoker([](const int& i) {return i+10;});
    auto f3 = Invoker(foo);
    auto f4 = Invoker(functor());
    auto f5 = Invoker([f1](int i) mutable {return f1(i);});
    return doit(f1);
}

Benchmark:

Benchmark

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.