std ::调用成员函数指针的替换失败

问题描述

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

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

现在奇怪的是,这似乎适用于没有参数的成员函数(例如下面的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,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,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,&testclass::num,"num"); // This compiles
    auto b2 = binder(w,"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,&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,Args...>;

template <class Obj,Ret (Obj::*) (Args...),Args...>;