2011-12-11 33 views
1

環境的幾點思考:微軟的Visual Studio 2010在C++模板特

編碼標準:C++ 0x中兼容

我有一個類模板

template <typename T1, int I> 
class A 
{ 
    public template <typename T2> void f(T2 x); 
    /*...*/ 
}; 

template <typename T1, int I> 
template <typename T2> 
void A<T1, I>::f(T2 x) 
{ 
    /*...*/ 
} 

和偏特以上類別

template <int I> 
class A<char, I> 
{ 
    public template <typename T2> void f(T2 x); 
    /*...*/ 
}; 

那我可不可以在下面的部分專業類中專門化成員函數?

template <int I> 
template <> 
void A<char, I>::f<double>(double x) 
{ 
} 

謝謝!

注意:我沒有處理它,但考慮它是否適用。如果您瞭解該規則,便可輕鬆評級。

+6

'public void'?你真的試圖編譯這個嗎? – Pubby

+0

@Pubby謝謝。對我的問題有任何建設性的想法? –

+1

您仍然沒有嘗試編譯......提示C++在可訪問性規範方面不是C#。 – Xeo

回答

1

這是無效的,因爲您不能明確地專門化一個成員函數,也不會給任何封閉類模板提供固定的模板參數。

不是C++ 11兼容,但MSVC

微軟編譯器的工作有一個擴展,允許雖然內類模板聲明明確專業。儘管我從來沒有嘗試過,有很好的機會,這將接受以下的非標準代碼

template <int I> 
class A<char, I> 
{ 
    public: 
    template <typename T2> void f(T2 x); 

    template<> void f<double>(double x) { 

    } 
    /*...*/ 
}; 

更新:鏘編譯此並報告

// clang++ -fms-extensions main1.cpp 
main1.cpp:10:21: warning: explicit specialization of 'f' within class scope is a 
       Microsoft extension [-Wmicrosoft] 

    template<> void f<double>(double x) { 
       ^

C++ 11/C++ 03兼容

的方法在這裏是超載的,而不是專業化

template <int I> 
class A<char, I> 
{ 
    public: 
    template <typename T2> void f(T2 x); 

    void f(double x) { 

    } 
    /*...*/ 
}; 
+0

我想,這段代碼給出了關於這個專業化的一個錯誤... – Yappie

+0

這就是我要找的,謝謝Johannes。然而,如果嵌入這個類的話,如果應用於構造函數,內聯的東西可能會有問題嗎? –

+0

我想內聯的東西有問題(inheretence,循環),並且即將接受重載解決方案。 –