2010-12-06 45 views
4

N3126警告:非常大的PDF)14.1/9,有兩種說法,使我困惑:「默認模板參數,可在模板聲明中指定」爲什麼mem func的默認模板參數應該明確定義爲非法?

#1:

#2:「默認模板參數不應在出現的成員的類之外的類模板的成員定義的模板參數列表中指定。」

#1意味着下面的代碼是合法的:

template <class T = int> 
void f(T = T()) 
{} 

int main() 
{ 
    int n = f(); // equivalent to f<int>() or f(0); 
    return 0; 
} 

#2意味着下面的代碼是非法的:

template <class T> 
struct X 
{ 
    template <class U = T> 
    void f(U a = U()) 
    {} 
}; 

int main() 
{ 
    X<int> x; 
    x.f(); // illegal, though I think it should be equivalent to x.f<int>() or x.f(0) 
    return 0; 
} 

我只是想知道爲什麼後者應該由標準明確定義爲非法?

原理是什麼?

+0

而不是「頁XXX」請說什麼節/段是。頁面引用無法使用,因爲不同的工作底稿會有大不相同的頁面,並且它是滾動到特定頁面的PITA。 – 2010-12-06 22:05:44

回答

3

我也可能錯了,但我的理解#2是,它使非法如下:

template<class T> 
struct A 
{ 
    void foo(); 
}; 

template<class T = int> 
void A<T>::foo() { /* ... */ } 
+0

非常感謝。英語不是我的母語。我必須承認我完全誤讀了這些陳述。 – xmllmx 2010-12-06 13:16:34

相關問題