這裏是憎惡如何在函數中默認構造迭代器的value_type作爲默認參數?
template <typename BidirIt, typename OutputIt, typename T,
typename BinaryDoOp, typename BinaryUndoOp>
void sliding_window(BidirIt first, BidirIt last, OutputIt d_first,
typename std::iterator_traits<BidirIt>::difference_type length,
T init = typename std::iterator_traits<BidirIt>::value_type(),
BinaryDoOp op = std::plus<>{},
BinaryUndoOp undo = std::minus<>{})
所以我想T
是默認和默認std::iterator_traits<BidirIt>::value_type
構建類型賦予它一個名稱init
的對象。
解決一個擬合線的一些變量類型的問題後,我發現,編譯器不能推斷T
,這裏是它究竟說:
error: no matching function for call to 'sliding_window' sliding_window(v.begin(), v.end(), output.begin(), window_length/, 0, std::plus<>(), std::minus<>()/);
note: candidate template ignored: couldn't infer template argument 'T' void sliding_window(BidirIt first, BidirIt last, OutputIt d_first,
我的編譯器是鐺++ - 3.9 。
呼叫站點代碼:
std::vector<int> v(window_length + window_count - 1);
std::iota(v.begin(), v.end(), 0);
std::vector<int> output(window_count);
std::vector<int> correct_result{3, 6, 9, 12};
sliding_window(v.begin(), v.end(), output.begin(), window_length/*, 0, std::plus<>(), std::minus<>()*/);
在評論部分是註釋掉的代碼工作正常。
根據我對模板的瞭解,它應該能夠推斷出該類型,因爲它實際上是一個默認構造函數調用,應該產生std::iterator_traits<BidirIt>::value_type
。我對功能模板化類型時默認參數類型的工作方式有任何誤解嗎?
問題:如何解決?也可以給它增加一些解釋。
感謝您提供更多信息。 Jarod的版本編譯正確,我相信它是標準一致的,所以可能會遺漏類型的rhs =是一個好主意? – Incomputable
@ Jarod42謝謝你的糾正。 –