范例解释:
#include#include #include #include #include using namespace std; template typename T::size_type fun(const T& t) { return t[0] * 2; } int main(int argc, char** argv) { fun(12); return 0; }
编译以上代码时产生了错误,也就是说类似这样的代码是错误的
int::size_type(const int &t)
{
//...
}
fun函数无法知道更合适的版本了,报错了,但是如果有合适的版本,即使产生了像上面一样的错误代码,编译器是忽略的。
#include#include #include #include #include using namespace std; template typename T::size_type fun(const T& t) { return t[0] * 2; } template typename T fun(const T& t) { return t * 2; } int main(int argc, char** argv) { fun(12); return 0; }
编译通过了,fun函数的实例化找到了合适的版本。



