13.9 Named Template Arguments

Ru-Brd

Section 16.1 on page 285 describes a technique that allows us to provide a nondefault template argument for a specific parameter without having to specify other template arguments for which a default value is available. Although it is an interesting technique, it is also clear that it results in a fair amount of work for a relatively simple effect. Hence, providing a language mechanism to name template arguments is a natural thought.

We should note at this point, that a similar extension (sometimes called keyword arguments ) was proposed earlier in the C++ standardization process by Roland Hartinger (see Section 6.5.1 of [StroustrupDnE]). Although technically sound, the proposal was ultimately not accepted into the language for various reasons. At this point there is no reason to believe named template arguments will ever make it into the language.

However, for the sake of completeness, we mention one syntactic idea that has floated among certain designers:

 template<typename T,           Move: typename M = defaultMove<T>,           Copy: typename C = defaultCopy<T>,           Swap: typename S = defaultSwap<T>,           Init: typename I = defaultInit<T>,           Kill: typename K = defaultKill<T> >  class Mutator {   };  void test(MatrixList ml)  {     mySort (ml, Mutator <Matrix, Swap: matrixSwap>);  } 

Note how the argument name ( preceding a colon ) is distinct from the parameter name. This allows us to keep the practice of using short names for the parameters used in the implementation while having a self-documenting name for the argument names. Because this can be overly verbose for some programming styles, one can also imagine the ability to omit the argument name if it is identical to the parameter name:

 template<typename T,           : typename Move = defaultMove<T>,           : typename Copy = defaultCopy<T>,           : typename Swap = defaultSwap<T>,           : typename Init = defaultInit<T>,           : typename Kill = defaultKill<T> >  class Mutator {   }; 
Ru-Brd


C++ Templates
C++ Templates: The Complete Guide
ISBN: 0201734842
EAN: 2147483647
Year: 2002
Pages: 185

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net