Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-universal-reference-and-perfect-forwarding-bug #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Edward-Elric233
Copy link

在阅读代码时发现代码中的完美转发是有问题的:因为模板类的成员方法只是一个普通的函数,并不是一个模板函数,所以他们的参数不会触发类型推导,所以对于T&&类型只是一个普通的右值引用。原本的代码只能接收右值引用参数,但是这显然不是代码的本意,因为后面使用了std::forward进行完美转发,因此应该是一个universal reference才对。测试代码如下:

#include <SignalSlotTrivial.h>
void compare1(string x, string y) {
    cout << "compare1" << endl;
}

int main() {
    SignalTrivial<void(string, string)> signal;
    function<void(string, string)> comapre3 = [](string x, string y) {
        cout << "compare2" << endl;
    };

    signal.connect(compare1);
    signal.connect([](string x, string y) {
        cout << "compare2" << endl;
    });
    //error:Rvalue reference to type 'function<...>' cannot bind to lvalue of type 'function<...>'
    signal.connect(comapre3);

    signal.call("Hello", "hello");
    string s1 = "Hello";
    string s2 = "hello";
    //error:Rvalue reference to type 'basic_string<...>' cannot bind to lvalue of type 'basic_string<...>'
    signal.call(s1, s2);

    return 0;
}

对于支持多线程实现同样也存在类似的问题。通过将connectcall函数都转换为模板成员函数就可以解决这个问题。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant