2011-06-09 52 views
5

我在編譯用下面的代碼時間麻煩:C++模板部分特例問題

template <typename T, 
      template <class T, class Allocator = std::allocator<T> > class C> 
    bool is_in(const C<T>& a, const C<T>& b); 

    template <typename T, std::vector> // HERE 
    bool is_in(const std::vector<T>& a, const std::vector<T>& b) 
    { 
    return false; // implementation tbd 
    } 

... 

vector<int> a, b; 

cout << is_in(a,b) << endl; 

該錯誤消息是(在標出「這裏」):

error: 'std::vector' is not a type 

(的當然,我包括向量從std!)。任何建議?我弄了一會兒,但我已經到了可以使用一些幫助的地步:-)我需要部分地專門化最初的模板聲明,以便我可以讓編譯器根據實際的類型容器C(將有一個is_in用於集合,一個用於矢量,一個用於範圍......,每次都有不同的算法)。

謝謝!

回答

6

標準不允許功能模板的部分特化。

一個簡單的解決方案是:使用過載。

template <typename T> 
bool is_in(const std::vector<T>& a, const std::vector<T>& b) 
{ 
    return false; // implementation tbd 
} 

這是重載函數模板。其非部分專業化。

或者,你可以這樣做:

namespace detail 
{ 
    template<typename T, typename C> 
    struct S 
    { 
     static bool impl(const C & a, const C & b) 
     { 
      //primary template 
      //... 
     } 
    } 
    template<typename T> 
    struct S<T, std::vector<T> > 
    { 
     static bool impl(const std::vector<T> & a, const std::vector<T> & b) 
     { 
      //partial specialization for std::vector 
      return false; 
     } 
    } 
} 

template <typename T, template <class T, class Allocator = std::allocator<T> > class C> 
bool is_in(const C<T>& a, const C<T>& b) 
{ 
    return detail::S<T, C<T> >::impl(a,b); 
} 
+1

一種解決方法是將功能的一類。 – 2011-06-09 20:02:21

+0

Booouuuhhhh! :-)當然,這將削減我的努力 - 我將不得不推出一個功能對象... – Frank 2011-06-09 20:03:14

0

我不知道,如果它(爲模板,模板始終是一個麻煩,我的心),但對於只是想

template <typename T> 
bool is_in(const std::vector<T>& a, const std::vector<T>& b) 
{ 
    ... 
} 

,因爲它是一個專業化。

編輯:其他人已澄清這一點,但我會補充它的完整性。上面的代碼實際上是一個重載而不是局部特化,但是不允許部分函數特化。

+1

這不是一個專業化。它的重載函數模板。 – Nawaz 2011-06-09 20:05:25

+0

@Nawaz哦,是的,你說得對。呂克的答案闡明瞭語法的差異,但是也不允許部分特化,正如你在答案中所說的那樣。 – 2011-06-09 20:16:54

1

功能模板部分專業化是不允許允許。在任何情況下,你都沒有使用模板特化語法,你實際上正在編寫一個額外的重載。試試這個:

template <typename T> 
bool is_in(const std::vector<T>& a, const std::vector<T>& b) 
{ 
    return false; // implementation tbd 
} 

如果偏特被允許,它應該是這樣的,而不是:

template <typename T> // std::vector is not a template parameter, 
         // so we wouldn't put it here 
bool is_in<T, std::vector>(const std::vector<T>& a, const std::vector<T>& b) 
// instead, it'd appear^here, when we're specializing the base template 
{ 
    return false; // implementation tbd 
} 
+1

這會導致:錯誤:函數模板部分特化'is_in 類std :: vector>'是不允許的。 – Frank 2011-06-09 20:07:37

+0

@Frank不要只是複製和粘貼代碼。儘管我在帖子中強調了這一點。 – 2011-06-09 20:09:52

+0

我沒有複製和粘貼。我早些時候曾經嘗試過。 – Frank 2011-06-09 20:10:55