[英]error C2783: '_Ty &&std::forward(remove_reference<_Ty>::type &&) throw()' : could not deduce template argument for '_Ty'
我有一個帶有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-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
,是“普通函數”,不能僅僅通過使用參數來恢復值類別。 兩個參數都是左值表達式。 您必須通過手動提供template-argument來指定要還原的值類別。
只有當我們有一個我們不知道先驗的參考是否是右值參考或左值參考時,這才有意義。 編寫使用帶轉發引用的 完美轉發的函數時就是這種情況。
順便說一句,我們想要恢復值類別,以允許另一個函數從我們收到的參數移開。 如果我們收到一個rvalue參數,我們想傳遞一個rvalue,以允許被調用的函數移動。
對於像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
的類型總是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&& x
形式的函數參數。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.