C++98中,类模板支持默认模板参数,但是函数模板并不支持,C++11则支持了函数模板的默认参数。如:
template<typename T = int>
class DefClass {}; //C++98编译通过,C++11编译通过
template<typename T = int>
void DefTempParm() {} //C++98编译失败,C++11编译通过
并且,类模板的默认参数必须从右往走定义,而函数模板则不做限制。如:
template<typename T1 = int, typename T2 = double, typename T3 = float> //编译通过
class DefClass1 {};
template<typename T1, typename T2 = double, typename T3 = float> //编译通过
class DefClass2 {};
template<typename T1 = int, typename T2, typename T3 = float> //编译失败
class DefClass3 {};
template<typename T1 = int, typename T2, typename T3 = float> //编译通过
void DefMethod(){}
网友评论