下面一段代碼在GCC 4.5.3中編譯,但不在VS 2008和2010中編譯。這是由於VS編譯器錯誤還是標準禁止提供默認函數模板參數值?成員函數模板參數默認值
#include <cstdlib>
struct Bar
{
enum Group{ A , B , C };
};
struct Foo
{
template<typename T>
static void getSome(typename T::Group = T::A);
};
template<typename T>
void Foo::getSome(typename T::Group)
{
};
int main()
{
Foo::getSome<Bar>(); // Does not compile in VS 2008 & 2010 (compiles in gcc 4.5.3)
Foo::getSome<Bar>(Bar::C); // Compiles in VS 2008 and gcc 4.5.3
return EXIT_SUCCESS;
}
錯誤消息
prog.cpp(11) : error C2589: '::' : illegal token on right side of '::'
prog.cpp(11) : error C2059: syntax error : '::'
另一個蹩腳的方法是隻提供帶默認參數的方法作爲模板方法的重載,即提供一個額外的'getSome()',它將調用'getSome(T :: A)'。這也適用於MSVC。 – 2013-08-22 12:55:10