Template's "Present" behavior * * * *
Any member in the template class can only be accessed or manipulated by an entity in the template class.
Point<float>::Status s; // ok
Point::Status s; // error
If we define a pointer, point to a specific entity, like this:
Point<float> *ptr = 0;
Since this is a pointer to class object and is not a class object in itself, the compiler does not need to know any members data associated with that class. So it is not necessary to have a float entity of point.
If it is not a pointer but a reference, suppose:
Point<float> &ref = 0;
The true semantics of this definition will be extended to:
// 内部扩展
Point<float> temp(float(0));
Point<float> &ref = temp;
The above conversion is because reference is not synonymous with nothing (no object), 0 is considered an integer and must be converted to an object of type point<float>.
However, member functions only when member functions is in use, C + + standard requires them to be "present". There are two main reasons for this rule:
(1) Space and efficiency considerations. It will take a lot of time and space for the unused function to be "present".
(2) features that have not yet been implemented. Not a template all types must be able to fully support a group of member functions, and thus need only a member functions that is really needed.
As an example:
Point<float> *p = new Point<float>;
Only the float instance (a) point template, (b) new operator, (c) default constructor need to be "present".
Template's Bug report * * *
All types-related tests, if they involve template parameters, must be deferred until the actual operation occurs.
For the following template declaration:
template <class T>
class Mumble
{
public:
Mumble(T t = 1024) : _t(t)
{
if(tt != t)
throw ex ex;
}
private:
T tt;
}
Which is like "t t = 1024", "TT!" A potential error such as "T" is not reported when template declaration, but is checked and recorded at the time of the occurrence of each active operation, and the result will vary depending on the actual type.
Mumble<int> mi; // 上述两个潜在错 误都不存在
Mumble<int*> pmi; // 由于不能将一个非零的整数常量指定给一个指针,故 “T t = 1024”错误