C++编程(B)

Educn 1352 shell less lessconst char templateclass

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: g University of Posts & Telecommunications Beijing young@buptnet.edu.cn 函数模板的参数示例 如果不能从模板函数的参数推断出某个模板参 数,那么就必须显式地去描述它 template<class T> class vector{/*...*/}; template<class T> T* create(); // 返回一个指向T的指针 void f() { vector<int> v; int* p = create<int>(); // 注意create后面的<int>参数 } Beijing University of Posts & Telecommunications Beijing young@buptnet.edu.cn 函数模板的参数示例 显式描述的最常见用途是为模板函数提供返回值 类型 可以将implicit_cast函数理解为隐式转换的一个 显式版本 template<class T, class U> T implicit_cast(U u) {return u;} void g(int i) { implicit_cast(i); // 错误:无法推断T implicit_cast<double>(i); // T是double,U是int implicit_cast<char,double>(i); // T是char,U是double,可以 implicit_cast<char*,int>(i); // T是char*,U是int // 错误,无法将int转换为char* } Beijing University of Posts & Telecommunications Beijing young@buptnet.edu.cn 13.3.2 函数模板的重载 可以声明多个具有同样名字的函数模板,甚 至可以声明具有同一个名字的多个函数模板 和常规函数的组合 template<class T> T sqrt(T); template<class T> complex<T> sqrt(complex<T>); double sqrt(double); void f(complex<double&g...
View Full Document

This note was uploaded on 02/10/2014 for the course ECE C++ taught by Professor Xiaoshengtang during the Fall '13 term at BUPT.

Ask a homework question - tutors are online