一.联合体可以使用模板
template <typename T>
union AllocChunk {
T object;
unsigned char bytes[sizeof(T)];
};
二.函数默认缺省调用实参
template <typename T>
void report_top (Stack<T> const&, int number = 10);
template <typename T>
void fill (Array<T>*, T const& = T()); // T() is zero for built-in types
缺省调用实参要保证有默认构造函数
class Value {
public:
Value(int); // no default constructor
};
void init (Array<Value>* array)
{
Value zero(0);
fill(array, zero); // OK: = T() is not used
fill(array); // ERROR: = T() is used, but not valid for T = Value
}
三.函数模板不可以声明为虚方法
template <typename T>
class Dynamic {
public:
virtual ~Dynamic(); // OK: one destructor per instance of Dynamic<T>
virtual void copy (){} //OK
template <typename T2>
virtual void copy (T2 const&);
// ERROR: unknown number of instances of copy()
// given an instance of Dynamic<T>
};
四.类模板的声明
后面不带尖括号
template<typename T> class Box; // OK: primary template
template<typename T> class Box<T>; // ERROR
template<typename T> void translate(T*); // OK: primary template
template<typename T> void translate<T>(T*); // ERROR
参考:http://www.cnblogs.com/stephen-liu74/archive/2012/08/27/2614249.html