Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

338
Views
How do I create a binding (Emscripten) for a function that accepts a `std::function &&`?

I have a function that takes a std::function as a parameter:

class Foo {
    virtual void bar(std::function<void()> &&func) = 0;
};

I want to create a JS wrapper that implements Foo.

struct FooWrapper : public wrapper<Foo> {
    EMSCRIPTEN_WRAPPER(FooWrapper);

    void bar(std::function<void()> &&func) override {
        call<void>("bar", std::move(func));
    }
};

EMSCRIPTEN_BINDINGS(Foo) {
        class_<Foo>("Foo")
                .smart_ptr<Foo>("Foo")
                .allow_subclass<FooWrapper, std::shared_ptr<FooWrapper>>("FooWraper", "FooWraperSharedPtr");
}

However, when I try to call bar from JavaScript (TypeScript), I get a BindingError.

const foo = // Create Foo object
foo.bar(() => {
    // Do something
})

BindingError: parameter 1 has unknown type NSt3__28functionIFvvEEE

Does anyone know how to bind a std::function so that I can pass a JavaScript lambda function to it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This actually is possible.

EMSCRIPTEN_BINDINGS(FUNCTION_VOID) {
            class_<std::function<void()>>("Function_Void")
                    .constructor()
                    .function("opCall", &std::function<void()>::operator());
    }

The JavaScript can then execute a std::function by simply calling opCall().

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!