std::invoke 成员函数指针替换失败

std::invoke substitution failure for member function pointer

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

所以,我写了一个 class 来存储对对象及其函数的引用,并且在构造函数中 webview::bind 是用解析字符串输入的 lambda 函数调用的,并调用功能。然后它将结果转换为字符串(我假设这将用于存在可以执行此操作的函数的情况)和 returns that.

现在奇怪的是,这似乎适用于没有参数的成员函数(例如下面的 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();
}

感谢您的帮助

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...>;