2013-05-20 51 views
2
// p1: some value 
// nt: native type 
template<int p1, typename nt> 
struct A {}; 

// pt: pointer to type i.e. int* 
// tA: a specialization of A 
template<typename pt, typename tA> 
struct B1 {}; 

// pt: pointer to type i.e. int* 
// tA: a specialization of A 
template<typename pt, typename tA> 
struct B2 {}; 

// tB: specialization of B? 
// tA: specialization of A 
template<typename tB, typename tA> 
struct C {}; 

// now i want to create a C partial specialization where: 
// A<some_value, native_type> 
// B?<char*, A<some_value, native_type> > 

template< template<typename, typename> class B, int p1, typename nt > 
struct C< B<char*, A<p1, nt> >, A<p1, nt> > {}; 

int main() 
{ 
    C< B1, A<10, int> > c; 
} 

當編譯上面的代碼鏗鏘它給人的錯誤:使用類模板沒有通過模板參數

error: use of class template B1 requires template arguments 
    C< B1, A<10, int> > c; 
     ^~ 

我理解錯誤,並修復它B1應該B1<char*, A<10, int> >。編譯器是否應該從最匹配的特化中扣除這些參數的值?

+0

這是什麼編譯器?有些模板模板參數有問題。 – StoryTeller

+0

ngng 3.2 for ubuntu –

+0

'C'需要兩種類型。所以說'C

回答

4

Should the compiler deduct the value of these parameters from the most matched specialization?

我不知道我理解你的問題,但如果我這樣做,那麼答案是「否」。編譯器不知道如何從類模板的裸名(本例中是B1)推導出一個完全實例化的類型(在這種情況下爲B1的專門化)。您必須指定B1的模板參數。

請記住,在爲主模板提供必要參數之後,會選擇主模板的特化。在這種情況下,您的主模板接受兩個類型參數,並且您必須提供兩個類型參數。

您正在使用模板的模板參數B在你的模板專業化爲了匹配類型參數,這些參數的B情況不會改變一個事實,即您的主模板的參數是實際上是兩個(完全實例)類型

+0

即使是模板模板參數?這似乎很奇怪。 – StoryTeller

+0

@StoryTeller:主模板接受兩個類型參數。模板模板參數僅用於主模板的專業化,用於描述這些類型參數中的第一個。 –

+1

啊!現在我明白了。錯過了它是一個專業化。 +1 – StoryTeller

相關問題