ISO: WG21/N0492 ANSI: 94-0105 Author: John Max Skaller Date: May 1994 Reply to: maxtal@suphys.physics.su.oz.au "COMPLETION OF TEMPLATE FACILITIES - SUMMARY" --------------------------------------------- The following items are possible additions to the template facilities. The list below is intended as a guide, not a full specification. 1) Namespace Templates. ----------------------- Example: template namespace X { int x; int f() { return x; } } X::x = 1; using namespace X; cout << f(); namespace X { void g() {} } X::g(); Semantics: A namespace template is similar to a class template with only static members. It differs in being extensible. Uses: An extensible set of related functions, types and variables can be defined or declared in a namespace and parameterised using templates. Unlike classes, these do not represent an object. Restrictions: None special to namespace templates. 2) Typedef Templates -------------------- Example: template typedef X Y; // make alias template class map { .. } template typedef map array; // partial closure template class set> class container { ..} container x; // typedef required for passing templates Semantics: A family of typedefs. Uses: This is an important facility, it is necessary for reordering and binding a subset of the formal parameters, to produce a form required for passing a template to a template. Required for creating an alias for a template. Generally useful for aliasing subsets of generic families. Restrictions: Each template formal must appear in both the LHS and RHS of the template typedef. 3) Template templates --------------------- Example: template template class X; X xil; Semantics: Equivalent to a single template with concatented formal parameter lists. Uses: Stylistic. 4) friend templates ------------------- Example: template class X { template friend class A; //FRIEND TEMPLATE // A is a friend of X for all T and U // MANY TO MANY friend class B; // single class B is a friend of each X // MANY TO ONE friend class C; // single class C is a friend of X // ONE TO ONE } Semantics: Declare a family of friends of a class. Note that a friend declaration in a class template declares a single friend. Only members are parameterised along with the class. Comment: Loss of encapsualtion is possible in combination with specialisations, however, the programmer has explicitly granted access to a family, which is sometimes necessary. For example: template class IntNumber { T value; template friend class FloatNumber; template friend class IntNumber; public: template void addFloat(const FloatNumber&); //member template }; template class FloatNumber { T value; template friend class IntNumber; template friend class FloatNumber; // friends public: template void addInt(const IntNumber&); //member template }; All the Int and Float Number classes are friends of each other. This may be necessary to define pairwise specialisations of members, in this case the add methods (which are themselves member templates).