2012-11-23 73 views
1

是否明確的專業化語法

template <typename T> 
void foo(T) {} 

template <> 
void foo(int) {} 

明確的專業化或函數重載,並明確初始化編譯想看看下面的代碼?

template <typename T> 
void foo(T) {} 

template <> 
void foo<int>(int) {} 

我認爲標準的同時接受這些:

ISO/IEC 14882:2011

14.7.3 Explicit specialization [temp.expl.spec] 

1 ... 

can be declared by a declaration introduced by template<>; that is: 
explicit-specialization: 
template < > declaration 

回答

2

要麼是允許的。在專業化

template <> 
void foo(int) {} 

編譯器從函數參數推斷模板參數T = int。從14.7.3p10:

拖地的模板參數可以留在模板id未指定命名的顯函數模板特殊化,只要它可以從函數參數類型推斷。

給出的例子涉及從類模板中扣除的,但它直接使用的類型是一樣適用於扣:

template<class T> class Array { /∗ ... ∗/ }; 
template<class T> void sort(Array<T>& v); 
// explicit specialization for sort(Array<int>&) 
// with deduced template-argument of type int 
template<> void sort(Array<int>&);