這是my previous question的延續。專門編譯器錯誤C3855(MSVC9)中的嵌套類靜態函數結果
我想創建一個嵌套類模板的類模板,其中嵌套類模板有一個靜態函數,我想提供一個專業化。
這裏是我的測試代碼,它沒有提供嵌套類的專業化。請注意空類NullType
的存在 - 這是我想要專門化嵌套類。
#include <cstdlib>
#include <iostream>
using namespace std;
class NullType {};
template<class FooParam> struct Foo
{
template<class BarParam> struct Bar
{
static bool Magic();
};
};
template<class FooParam>
template<class BarParam>
bool Foo<FooParam>::Bar<BarParam>::Magic()
{
return true;
}
int main()
{
bool b = Foo<int>::Bar<int>::Magic();
cout << b << " ";
bool b2 = Foo<int>::Bar<NullType>::Magic();
cout << b2 << " ";
}
此代碼編譯並按預期運行。輸出是:
1 1
現在我想提供NullType
的Bar::Magic
一個專業化,有函數返回false
。所需的輸出是:
1 0
所以我試圖寫的專業化是這樣的:
template<class FooParam>
template<>
bool Foo<FooParam>::Bar<NullType>::Magic()
{
return false;
} // COMPILER ERROR HERE C3855
這個定義Foo::Bar::Magic()
現有定義之前出現。
然而,該代碼產生一個編譯錯誤:
1>.\main.cpp(20) : error C3855: 'Foo<FooParam>::Bar<BarParam>': template parameter 'BarParam' is incompatible with the declaration
...的閉括號上面所指出的。
我可以用什麼語法來提供這種專業化?我願意接受任何和所有的技術來實現我的主要目標(其他一切返回false
爲NullType
和true
),只要:
- 無需外部庫使用(增強)
- 它採用兼容標準的C++只有
Foo
是一個類模板,Bar
是嵌套類模板或函數模板。返回false
或true
的函數必須是特殊化的或可重載的,以便調用NullType
的代碼返回false
,但其他所有操作都將返回true
。
我會在需要時澄清要求。
根據14.7.3/18, 構件模板不能被其包圍類模板之前專門 是專門。 所以,你的要求可能無法得到滿足,因爲它是... – 2011-05-01 19:59:40