2017-07-31 85 views
-5

我想用std::make_pair()<weak_ptr, string>,錯誤的是:如何make_pair包括了weak_ptr

/usr/include/c++/4.8/bits/stl_pair.h:276:5: note: template<class _T1, class _T2> constexpr std::pair<typename std::__decay_and_strip<_Tp>::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) 
    make_pair(_T1&& __x, _T2&& __y) 
    ^
/usr/include/c++/4.8/bits/stl_pair.h:276:5: note: template argument deduction/substitution failed: 
.build/experimental_trusty/data_provider/Dataserver.cc:112:109: note: cannot convert 'hdl' (type 'websocketpp::connection_hdl {aka std::weak_ptr<void>}') to type 'std::weak_ptr<void>&&' 

爲什麼?如何解決它?

+6

請製作一個[最小,完整和可驗證的示例](https://stackoverflow.com/help/mcve),並顯示您已經完成的編碼嘗試。 – Akira

回答

1

你沒有向我們展示你的代碼,但根據你發佈的錯誤,這是我的猜測。

此外,請研究更好。我在這個網站上幾次看到過這個同樣的問題。順便說一句,this documentation就如何使用該功能很好的例子。

我的猜測是,你有這樣的代碼:

auto p = std::make_pair<websocketpp::connection_hdl, std::string>(hdl, str); 

這不是調用此函數的正確途徑。

實際上,std::make_pair主要用於模板參數推導。這是編譯器根據發送給函數的參數推導出模板參數應該是什麼的機制。

例如,如果我這樣寫:

template<typename T> 
void foo(T) {} 

我可以調用它像:

foo(4.7); 

編譯器會推斷Tdouble

在你的情況,你應該這樣寫:

auto p = std::make_pair(hdl, str); 

而讓演繹發生。