我的工作Visual Studio 2015 community edition
函數模板(它是類模板的成員)的顯式特化產生「不允許部分特化」錯誤,爲什麼?
讓我們說我有,像這樣一個簡單的類:
(下面的例子「應該是」可編譯的,因爲它包括所有必要的東西,不幸的是,它會產生一個錯誤)。
#include <stdexcept>
template <typename T>
class class_foo
{
// members, methods, constructors. not important stuff...
// helper functions:
private:
class tag_aaa {}; // to resolve few things at compile-time, not run-time.
class tag_bbb {}; // - || -
template <typename tag>
void erase();
// for some reason this is not interpreted as an error by my compiler:
template<>
void erase<tag_aaa>();
template<>
void erase<tag_bbb>();
};
// catch-all-do-nothing "version"
// well, catch-all-throw-an-exception because call to this function is an obvious error.
// that should never occur.
template <typename T>
template <typename tag> inline
void class_foo<T>::erase()
{
throw std::runtime_error("Very weird error...");
}
template <>
template <typename T> inline
void class_foo<T>::erase<class_foo<T>::tag_aaa>()
{
// do some stuff...
}
template <>
template <typename T> inline
void class_foo<T>::erase<class_foo<T>::tag_bbb>()
{
// do some stuff...
}
int main()
{
class_foo<double> bar;
return 0;
}
錯誤:
1>D:/develop/workspace/visual_studio/nevada_test_site/source/workspace/nevada_test_site/start.cu(36): error : partial specialization of class "class_foo<T>::erase<class_foo<T>::tag_aaa> [with T=T]" is not allowed
1>D:/develop/workspace/visual_studio/nevada_test_site/source/workspace/nevada_test_site/start.cu(43): error : partial specialization of class "class_foo<T>::erase<class_foo<T>::tag_bbb> [with T=T]" is not allowed
1>D:/develop/workspace/visual_studio/nevada_test_site/source/workspace/nevada_test_site/start.cu(51): warning : variable "bar" was declared but never referenced
我想到自己作爲一個初中愛好者程序員,所以肯定是我錯了,但我相信,這兩個erase<class_foo<T>::tag_aaa>()
和erase<class_foo<T>::tag_bbb>()
是template <typename tag> void erase();
功能明確的專業。因此,他們是被允許的。我相信這個錯誤是由於語法錯誤,但我找不到錯誤。
問:
- 是什麼,我試圖做的,是否允許?
- 如果是,我做錯了什麼?
- 如果是,專門用於此功能的正確語法是什麼(
erase
)?
是否有任何人願意幫助我嗎? – cukier9a7b5