任何人都可以解釋爲什麼在第三版C++編程語言的第13章中,Stroustrup說明了函數模板的默認參數,但它們不受C++(pre C++ 11)支持嗎?這是在第13.4.1由斯特勞斯給出的例子:爲什麼Stroustrup的書會演示當時不允許的默認函數模板參數?
Explicitly specifying the comparison for each call is tedious. Fortunately, it is easy to pick a default so that only uncommon comparison criteria have to be explicitly specified. This can be implemented through overloading:
template<class T, class C> int compare(const String<T>& str1, const String<T>& str2); // compare using C template<class T> int compare(const String<T>& str1, const String<T>& str2); // compare using Cmp<T>
Alternatively, we can supply the normal convention as a default template argument:
template <class T, class C = Cmp<T> > int compare(const String<T>& str1, const String<T>& str2)
,這是編譯器錯誤:
error: default template arguments may not be used in function templates
大概說明你不能做什麼。但是,你可以在課堂上做到這一點。 – AJG85
如果你知道它們不被Standard支持,並且Stroustrup仍然可以在書中添加它,可能只有Stroustrup可以說明原因。 –
@ AJG85不,他在那裏說明你*可以*做什麼,而不是你不能做什麼。 – Martin