c – 错误C2783:’_Ty \u0026\u0026 std :: forward(remove_reference :: type \u0026\u0026)throw()’:无法推断

c – 错误C2783:’_Ty \u0026\u0026 std :: forward(remove_reference :: type \u0026\u0026)throw()’:无法推断,第1张

概述我有一个带有push函数的并发队列的模板化实现,如下所示: template <typename T>class concurrent_queue{public: // other code... void push(const T& item) { std::unique_lock<std::mutex> mlock(mutex); 我有一个带有push函数的并发队列的模板化实现,如下所示:

template <typename T>class concurrent_queue{public:    // other code...    voID push(const T& item)    {        std::unique_lock<std::mutex> mlock(mutex);        queue.push_back(std::forward(item));        mlock.unlock();        notEmpty.notify_one();    }private:    std::deque<T>               queue;    std::mutex                  mutex;    // other stuff...};

稍后,我实例化它并像这样使用它:

concurrent_queue<c2Type> m_queue;  // c2 type is some struct declared prevIoUsly

然后我尝试推送队列上的项目,我得到上面提到的编译器错误

c2Type c2message;// fill in the message struct...m_queue.push(c2message);

我之前成功地使用了队列作为线程池实现的一部分,它存储了std :: function对象.我不明白为什么在这种情况下不能推断出类型.有什么想法吗?

解决方法 像“左值”和“右值”这样的值类别是表达式的属性.名称变量的表达式总是lvalue表达式,即使它们命名的变量具有对some_type的类型rvalue引用.

我们使用lvalue-reference和rvalue-references来绑定不同类别的表达式:按照惯例,我们将lvalue-references视为绑定到lvalues,并将rvalue-references视为绑定到rvalues.

std :: forward旨在恢复我们假设引用所引用的值类别.例如:

int   i = 42;int&  l = i;int&& r = 21;l // this Expression is an lvalue-Expressionr // this Expression is an lvalue-Expression,too (!)std::forward<int& >(l) // this function-call Expression is an lvalue-Expressionstd::forward<int&&>(r) // this function-call Expression is an rvalue-Expression

std :: forward,是“普通函数”,不能仅仅通过使用参数来恢复值类别.两个参数都是左值表达式.您必须通过手动提供template-argument来指定要还原的值类别.

只有当我们有一个我们不知道先验的参考是否是右值参考或左值参考时,这才有意义.编写使用带转发引用的完美转发的函数时就是这种情况.

顺便说一句,我们想要恢复值类别,以允许另一个函数从我们收到的参数移开.如果我们收到一个rvalue参数,我们想传递一个rvalue,以允许被调用的函数移动.

对于像OP中那样的函数:

voID push(const T& item)

我们知道item有对const T的类型lvalue引用.因此,我们不需要std :: forward:

voID push(const T& item) {    // ...    queue.push_back(item); // pass the lvalue argument as an lvalue    // ...}

如果我们添加另一个重载:

voID push(T&& item)

我们仍然不需要std :: forward,因为该参数项的类型总是rvalue-reference to T(假设T不是引用类型):

voID push(T&& item) {    // ...    queue.push_back(std::move(item)); // pass the rvalue argument as an rvalue    // ...}

只有我们有类似的东西

template<typename U>voID push(forwarding_reference<U> item)

其中forwarding_reference< U>可以是左值引用或右值引用,那么我们需要std :: forward:

template<typename U>voID push(forwarding_reference<U> item) // not C++,read on{    // ...    queue.push_back(std::forward<U>(item)); // pass lvalue arguments as lvalues                                            // and rvalue arguments as rvalues    // ...}

由于实施细节,我们必须将上述内容写成:

template<typename U>voID push(U&& item) {    // ...    queue.push_back(std::forward<U>(item)); // pass lvalue arguments as lvalues                                            // and rvalue arguments as rvalues    // ...}

注意上面的U&& item不是rvalue-reference,而是转发引用.要获得转发参考,您需要一个带有一些模板类型参数X的函数模板和一个X&&amp ;;形式的函数参数. X.

总结

以上是内存溢出为你收集整理的c – 错误C2783:’_Ty \u0026\u0026 std :: forward(remove_reference :: type \u0026\u0026)throw()’:无法推断’_Ty’的模板参数全部内容,希望文章能够帮你解决c – 错误C2783:’_Ty \u0026\u0026 std :: forward(remove_reference :: type \u0026\u0026)throw()’:无法推断’_Ty’的模板参数所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1216445.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存