++ implicitly converts the operands' types in expressions that involve different datatypes. Here's a few examples:
double d=n; //5 implicitly converted to 5.0
if (n==d) //same here
Many programmers don't even know that these implicit conversions
take place because they are so trivial and intuitive. However, when templates are involved, it's a different story. Consider the following:
The assignment of Array<int>
fails because there is no standard conversion between these two independent types.
How do you simulate the implicit conversion of built-in datatypes with different specializations of the same template?
Add an asymmetric assignment operator to the class template.
| Notes on Terminology: The terms "template member" and "member template" sound like synonyms. They aren't. A template member is a member of a class template. For example:
By contrast, a member template is a template declared within a class or class template. This 10-Minute Solution uses a member template to overcome the lack of implicit conversions between different specializations of the same class template.
template <classt T> class Array
T * p; // p is a template member
void func(T &) const; // so is func