1

我正在使用 webview(在这里找到:https ://github.com/webview/webview )来创建一个带有 html/js gui 的 c++ 程序。为了从 js 调用 c++ 函数,它们必须是std::string function(std::string). 这对于自由函数来说是相当微不足道的,但是如果你想将指针传递给成员函数,它似乎并不那么微不足道。

因此,我编写了一个类,它存储对对象及其函数的引用,并在构造函数中调用 webview::bind,并使用解析字符串输入的 lambda 函数调用该函数。然后它将结果转换为字符串(我假设这将用于存在可以执行此操作的函数的情况)并返回。

现在奇怪的是,这似乎适用于没有参数的成员函数(例如下面的 testcase::num),但如果我包含一个带参数的成员函数,我会收到错误:

testcase.cpp:14:22: error: no matching function for call to 'invoke'
            auto r = std::invoke(f, o, std::forward<Args>(get<Args>(args))...);
                     ^~~~~~~~~~~
testcase.cpp:55:15: note: in instantiation of member function 'binder<testclass, int (testclass::*)(int, int)>::binder' requested here
    auto b2 = binder(w, tc, &testclass::add, "add");
              ^
/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/functional:2845:1: note: candidate template ignored: substitution failure [with _Fn = int
      (testclass::*&)(int, int), _Args = <testclass &>]: no type named 'type' in 'std::__1::invoke_result<int (testclass::*&)(int, int), testclass &>'
invoke(_Fn&& __f, _Args&&... __args)
^

下面的示例代码是在 Mac 上编译的 g++ testcase.cpp -o testcase -std=c++17 -framework WebKit

#include <string>
#include <sstream>
#include <functional>

#include <iostream>
#include "webview.h"

template<class Obj, class F, class ...Args>
class binder{
public:
    binder(webview::webview &w, Obj& obj, F func, std::string name) : _w(w), o(obj), f(func) {
        _w.bind(name, [&](std::string s)->std::string {
            std::stringstream args(s);
            auto r = std::invoke(f, o, std::forward<Args>(get<Args>(args))...);
            return std::to_string(r);
        });
    }

private:
    template<class T>
    static T get(std::istream& args){
        T t; // must be default constructible
        if(!(args >> t)){
            args.clear();
            throw std::invalid_argument("invalid argument to stream_function");
        }
        return t;
    }

    Obj& o;
    F f;

    webview::webview& _w;
};

class testclass {
public:
    int add (int a, int b) {
        return a+b;
    }
    int num () {
        return 5;
    }
};

int main() {

    webview::webview w(true, nullptr);
    w.set_title("test");
    w.set_size(1200, 800, WEBVIEW_HINT_NONE);


    testclass tc;
    auto b1 = binder(w, tc, &testclass::num, "num"); // This compiles
    auto b2 = binder(w, tc, &testclass::add, "add"); // This doesn't 

    w.navigate(R"(data:text/html,
        <!doctype html>
        <html>
          <body>
          <div id='num'></div>
          <div id='add'></div>
          </body>
          <script>
            window.onload = function() {
              num('hello').then(function(res) {
                document.getElementById('num').innerHTML = res;
                console.log('num res', res);
              });
              add(1, 2).then(function(res) {
                document.getElementById('add').innerHTML = res;
                console.log('add res', res);
              });
            };
          </script>
        </html>
      )");
    w.run();
}

谢谢你的帮助

4

1 回答 1

1

auto b1 = binder(w, tc, &testclass::num, "num"); // This compiles
auto b2 = binder(w, tc, &testclass::add, "add"); // This doesn't

您使用CTAD (C++17),但来自:

template <class Obj, class F, class ...Args>
class binder{
public:
    binder(webview::webview &w, Obj& obj, F func, std::string name);
    // ...
};

Args...无法推断,因此是一个空包。

您可以添加您的演绎指南来解决这个问题:

template <class Obj, class Ret, class ...Args>
binder(webview::webview &, Obj&, Ret (Obj::*) (Args...) const, std::string) -> binder<Obj, Ret (Obj::*) (Args...) const, Args...>;

template <class Obj, class Ret, class ...Args>
binder(webview::webview &, Obj&, Ret (Obj::*) (Args...), std::string) -> binder<Obj, Ret (Obj::*) (Args...), Args...>;
于 2020-08-29T11:31:57.063 回答