TITLE: a tip for multidimensional diehards (Newsgroups: comp.lang.c++.moderated) PROBLEM: Martin Aupperle (100754.2730@compuserve.com) The requirement is that one can write Shape s; s[1][2][3]; but not s[1][2]; or s[1][2][3][4]; So we neeed some clever declaration of operator []. Since we do not want to have manual instantiations, we need an automatic (i.e. recursive) instantiation. With a helper class, this could be template< class T, int n > struct Helper { Helper operator[] ( T ); /* ... more of Helper ? ... */ }; and in Shape: Helper operator [] ( T ); but when I write Shape s; s[1][2][3]; this gives, of course, an endless recursion in the compiler when trying to instantiate Helper's operator []. RESPONSE: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl), 8 June 96 Sure: You need to terminate the recursion somewhere. This is done with (partial) specialization: template struct Helper { void operator[](T); }; However, partial specialization is currently not available for most (all?) compilers. You can get around this problem, using full specialization instead: If you want to use 'Shape' you have to spezialize 'Helper', e.g. for your example where 'T' == 'int': struct Helper { void operator[](T); }; Having said that partial specialization is not available, I want to note that I'm not sure whether the syntax used for the partial specialization above is correct. However, I think it is but I can't test it with a compiler. A while ago I posted sources for a multi-dimensional array class using a similar approach. I have refined it and although partial specialization is not available I can now use it with any time with a little help from the user: The user has to call a macro like 'MULTI_ARRAY(type)' if he want to use the multi-array class to store objects of type 'type'. If you are interested in this class, just send me an e-mail. RESPONSE: vandevod@cs.rpi.edu (David Vandevoorde) The partial specialization syntax seems right to me. Ironically, the full specialization syntax is no longer conforming. Though most compilers still require it the way you wrote it, the DWP syntax is now: template<> struct Helper { void operator[](T); }; [...]