【问题标题】:error C2783: '_Ty &&std::forward(remove_reference<_Ty>::type &&) throw()' : could not deduce template argument for '_Ty'错误 C2783:“_Ty &&std::forward(remove_reference<_Ty>::type &&) throw()”:无法推断“_Ty”的模板参数
【发布时间】:2014-12-29 21:38:56
【问题描述】:

我有一个并发队列的模板化实现,它具有如下所示的推送功能:

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 对象。我不明白为什么在这种情况下它不能推断出类型。有什么想法吗?

【问题讨论】:

  • queue.push_back(std::forward(item)); 没有理由转发 对常量 T 的引用。 -- 错误的原因是您必须手动为std::forward 提供模板参数:std::forward&lt;T&gt;(t),因为它(旨在)“恢复”t 的值类别,如果t 是转发参考。
  • 谢谢。我删除了 const 并添加了模板参数,它解决了问题。如果您将评论添加为答案,我会接受。
  • 我认为您应该使用void push(T&amp;&amp; item),不使用const 并作为通用参考(&amp;&amp;)。并使用明确的T 调用forward&lt;T&gt;。如果您想保留 const,您还应该将其添加到对forward&lt;const T&gt; 的调用中。请参阅isocpp.org/blog/2012/11/… 了解有关不同类型引用的混乱的更多详细信息。

标签: c++ c++11 perfect-forwarding forwarding-reference


【解决方案1】:

像“左值”和“右值”这样的值类别是表达式的属性。命名变量的表达式总是左值表达式,即使它们命名的变量的类型为 rvalue reference to some_type

我们使用左值引用和右值引用来绑定不同类别的表达式:按照惯例,我们将左值引用视为被绑定到左值,而右值引用被视为被绑定到右值

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

int   i = 42;
int&  l = i;
int&& r = 21;

l // this expression is an lvalue-expression
r // this expression is an lvalue-expression, too (!)

std::forward<int& >(l) // this function-call expression is an lvalue-expression
std::forward<int&&>(r) // this function-call expression is an rvalue-expression

std::forward,作为一个“普通函数”,仅仅通过参数是无法恢复值类别的。两个参数都是左值表达式。您必须通过手动提供模板参数来指定要恢复的值类别。

这只有在我们有一个我们不知道先验是右值引用还是左值引用的引用时才有意义。编写一个使用完美转发转发引用的函数时就是这种情况。

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


对于类似 OP 中的功能:

void push(const T& item)

我们知道item 具有类型const T的左值引用。因此,我们不需要std::forward

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

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

void push(T&& item)

我们仍然不需要std::forward,因为该参数item 的类型始终是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&lt;U&gt; 可以是左值引用右值引用,那么我们需要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&amp;&amp; item 不是右值引用,而是转发引用。要获得转发引用,您需要有一个带有模板类型参数XX&amp;&amp; x 形式的函数参数的函数模板。

【讨论】:

  • g++ 顺便接受template&lt;typename T&gt; using forwarding_reference = T&amp;&amp;; 作为转发引用,但clang++ 不接受。 :(
  • 很棒的答案,这真的消除了我在其他 SO 答案中没有看到的关于转发的一些误解
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多