2014-02-10 44 views
0

我試圖創建一個重載的方法,其中兩個模板。一個需要4個參數,一個需要5。但是我相處的將函數模板與另一個函數模板重載是否合法?

Error C2780 ... OutOfPlaceReturn ... : expects 4 arguments - 5 provided. 

... A bunch of template parameters ... 

See declaration of ' ... ::OutOfPlaceReturn' 

它引用了4參數方法定義

線在這種情況下,線的錯誤,我試圖調用重載5個參數,所以我不明白爲什麼編譯器認爲我想調用只有4個參數的函數。

完整的上下文是太複雜,給予充分的代碼示例,但我只想說,這一切都需要一個類模板,它有很多的地方typedef S,其中包括samp_typeconst_sampsamp_vec等,這些裏面的地方是一個模板參數,它擁有一個POD類型,或這些POD類型之一的std::array全部或者類型定義

typedef int_fast16_t        fast_int; 
typedef typename std::add_const<fast_int>::type const_fast_int; 

typedef samp_type (*func_type)(const_samp, const_samp); 

template<func_type operation, const_fast_int strideA, const_fast_int strideB, const_fast_int strideOut> 
static inline samp_vec OutOfPlaceReturn(const std::array<samp_type, strideA * vectorLen> &a, 
             const_fast_int strideA, 
             const std::array<samp_type, strideB * vectorLen> &b, 
             const_fast_int strideB, 
             const_fast_int strideOut) 
{ 
    std::array<samp_type, vectorLen * strideOut> output; 
    for(fast_int i = 0; i < vectorLen; ++i) 
     output[i * strideOut] = operation(a[i * strideA], b[i * strideB]); 
    return output; 
} 
template<func_type operation, const_fast_int strideA, const_fast_int strideOut> 
static inline samp_vec OutOfPlaceReturn(const std::array<samp_type, strideA * vectorLen> &a, 
             const_fast_int strideA, 
             const_samp_ref b, 
             const_fast_int strideOut) 
{ 
    std::array<samp_type, vectorLen * strideOut> output; 
    for(fast_int i = 0; i < vectorLen; ++i) 
     output[i * strideOut] = operation(a[i * strideA], b); 
    return output; 
} 

如果我理解正確,調用模板函數時,你不需要提供模板編譯器可以通過函數參數推導出的參數,所以調用如下所示

static samp_vec subtract(const_vec_ref a, const_fast_int strideA, const_vec_ref b, const_fast_int strideB, const_fast_int strideOut) 
{ return OutOfPlaceReturn<MathClass::subtract>(a, strideA, b, strideB, strideOut); } 

那麼,我調用這些模板方法的方式有什麼問題嗎?我希望編譯器解決重載的方式有什麼問題嗎?

編輯

我使用VS2010。到目前爲止,模板和C++ 11數據類型都非常好。不知道我的編譯器是否正在執行sub-par

+0

不要認爲它解釋了錯誤,但擁有模板參數'strideA'和函數參數'strideA'並不是一個好主意。您可能可以放棄函數參數,只使用定義內的模板參數。 – aschepler

+0

爲什麼'static'?.. – Potatoswatter

+0

我嘗試從函數參數中刪除strideA,strideB和strideOut。編譯器仍然抱怨,但這次有點不同:'太多的模板參數'。這在我看來基本上是同樣的問題:編譯器無法弄清楚我的意思是指哪種模板方法。 – xaviersjs

回答

1

重載解析僅適用於可實例化的模板。這是爲什麼SFINAE工作的一部分。

在你的情況,你的5 arg超載有一個含糊strideOut。那是一個模板參數還是不是?

+0

實際上,'strideOut'(它們都在)中的函數參數聲明是無效的,因爲模板參數的名稱不應該在模板中重新聲明。 – Potatoswatter

+0

@Patatoswatter:現在我看看它,'strideA'和'strideB'也是。而4-arg版本與'strideOut'和'strideA'幾乎有相同的問題。我想編譯器只是放棄了。 – MSalters

+0

此外,他似乎想要'strideA * vectorLen'被推斷,也許是通過隱式分割。最好的建議可能是一次只採取一步:) – Potatoswatter