簡體   English   中英

具有功能類型的模板參數原因編譯器錯誤

[英]Templates with Function Type Parameter Cause Compiler Error

我在C ++中定義了以下函數:

template<class Type> Type GetMedian(const vector<Type>& items, function<bool(Type, Type)> comp) {
  vector<Type> copied_items(items);
  std::nth_element(copied_items.begin(), copied_items.begin() + copied_items.size()/2, copied_items.end(), comp);
  return copied_items[copied_items.size()/2];
}

但是,當我嘗試將其稱為GetMedian(v, greater<uint32_t>()) ,我的編譯器(clang)會抱怨:

error: no
      matching function for call to 'GetMedian'
  GetMedian(v, greater<uint32_t>());
  ^~~~~~~~~
note: 
      candidate template ignored: could not match 'function' against 'greater'
template<class Type> Type  GetMedian(const vector<Type>& items, function...

但是,每當我更改為不使用模板時,我都沒有看到此錯誤,如:

uint32_t GetMedian(const vector<uint32_t>& items, function<bool(uint32_t, uint32_t)> comp) {
  vector<uint32_t> copied_items(items);
  std::nth_element(copied_items.begin(), copied_items.begin() + copied_items.size()/2, copied_items.end(), comp);
  return copied_items[copied_items.size()/2];
}

有沒有辦法讓我的功能像我想的那樣靈活?

Type在這里有兩個點:

template<class Type> 
Type GetMedian(const vector<Type>& items, function<bool(Type, Type)> comp);
                            ^^^^                        ^^^^^^^^^^

當你用GetMedian(v, greater<uint32_t>())調用它時,它會為v推導出Typeuint32_t ,但是它需要從greater<uin32_t>推導出function<bool(Type, Type)> greater<uin32_t> 但后者不是類型function ,因此推論失敗。 它可以轉換function<bool(uint32_t, uint32_t)> ,但在模板推導過程中不會發生轉換。

謝天謝地,你實際上並不需要這里的std::function 它實際上更糟糕 - 你無緣無故地給自己帶來類型擦除的開銷。 只需將比較器作為單獨的模板類型:

template <class Type, class Comp>
Type GetMedian(const vector<Type>& items, Comp comp);

或者,如果你真的真的想要一個std::function ,你可以通過以下方式將Type包裝在一個非推導的上下文中:

template <class T> struct non_deduced { using type = T; };
template <class T> using non_deduced_t = typename non_deduced<T>::type;

template <class T>
T median(const std::vector<T>&, std::function<bool(non_deduced_t<T>, non_deduced_t<T>)>)

現在,允許從std::greater<uint32_t>std::function<bool(uint32_t, uint32_t)>的轉換,因為它只是vector<T> ,它是推導的上下文,因此編譯器將T推導為uint32_t然后檢查第二個參數轉換是否有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM