最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Calling javascript function in c++ with emscripten embind - Stack Overflow

programmeradmin2浏览0评论

This question es in two parts. What I want to do is to put most of my program logic in c++ classes and some view related function in js (like DOM manipulation and styling.) I use emscripten embind the classes and it works fine while I don't know how to interact with the js code (there are really limited resources on their tutorial.)

I was thinking to pass a val object to the c++ class according to their tutorial () The passing works just fine while the "call" function doesn't work. I got a pile time error.

Here is the example I tried which they put on their tutorial:

#include <emscripten/val.h>
using namespace emscripten;

int main(){
  val Math = val::global("Math");
  Math.call("abs",-10);
  return 0;
}

and I got the following errors:

error: no matching member function for call to 'call'
    Math.call("abs",-10);
    ~~~~^~~~
emscripten/1.5.6/system/include/emscripten/val.h:247:21: note: candidate template ignored: couldn't infer template argument 'ReturnValue'
    ReturnValue call(const char* name, Args&&... args) const {

Basically it says the the piler doesn't know the return type of the "call" function. Did I do anything wrong or is there a better way to interact with js code?

Thanks, yi

This question es in two parts. What I want to do is to put most of my program logic in c++ classes and some view related function in js (like DOM manipulation and styling.) I use emscripten embind the classes and it works fine while I don't know how to interact with the js code (there are really limited resources on their tutorial.)

I was thinking to pass a val object to the c++ class according to their tutorial (https://github./kripken/emscripten/wiki/Tutorial) The passing works just fine while the "call" function doesn't work. I got a pile time error.

Here is the example I tried which they put on their tutorial:

#include <emscripten/val.h>
using namespace emscripten;

int main(){
  val Math = val::global("Math");
  Math.call("abs",-10);
  return 0;
}

and I got the following errors:

error: no matching member function for call to 'call'
    Math.call("abs",-10);
    ~~~~^~~~
emscripten/1.5.6/system/include/emscripten/val.h:247:21: note: candidate template ignored: couldn't infer template argument 'ReturnValue'
    ReturnValue call(const char* name, Args&&... args) const {

Basically it says the the piler doesn't know the return type of the "call" function. Did I do anything wrong or is there a better way to interact with js code?

Thanks, yi

Share Improve this question edited Apr 15, 2022 at 20:59 Tim Sylvester 23.2k2 gold badges81 silver badges98 bronze badges asked Dec 2, 2013 at 4:24 yi chenyi chen 2414 silver badges11 bronze badges 2
  • Did you try assigning the return value to something so the piler has a hint? The other thing you could try explicitly passing the template argument. – Jerry Jeremiah Commented Dec 2, 2013 at 4:40
  • I tried to assign the returned value to some variable with static type but that doesn't work either. – yi chen Commented Dec 2, 2013 at 5:08
Add a ment  | 

1 Answer 1

Reset to default 7

That's a mon C++ problem. As a general rule, the following message should always make you double check in C++:

note: candidate template ignored: couldn't infer template argument 'ReturnValue' ReturnValue call(const char* name, Args&&... args) const

This mostly means that you tried to call a templated function but did not specify the necessary types.

If you look at the signature (in system/include/emscripten/val.h):

template<typename ReturnValue, typename... Args>
ReturnValue call(const char* name, Args&&... args) const

While it can infer Args quite fine, it has no idea, what ReturnValue might be. So calling this function should be done via e.g.:

Math.call<int>("abs",-10);
发布评论

评论列表(0)

  1. 暂无评论