Topic: Another problem with templates...


Author: bs@alice.att.com (Bjarne Stroustrup)
Date: 8 Feb 94 22:27:47 GMT
Raw View
 moudgill@cs.cornell.edu ( Mayan Moudgill @ Cornell Univ. CS Dept, Ithaca NY 14853) writes

 > One can define a template class with an constant-expression parameter, such as:
 >
 >    template <int size> class Vector {
  >       int Vector[size];
 >    };
 >
 > However, I have not figured out how to write any non-member functions
 > that can use this type.

To be precise, non-member template functions.

 > All template-function arguments must be a type-argument;
 > so template functions are ruled out.
 > Specifically, I can't write:
 >
 >    template <int size> ostream& operator<<(ostream& o, Vector<size> v)
 >    {
 >    }
 >
 >
 > Just to make it totally bizarre....
 > It _IS_ possible to do this
 >
 >    template <int size> class Vector {
 >        int Vector[size];
 >        friend ostream& operator<<(ostream& o, Vector<size> v)
 >   {
 >   }
 >    };
 >
 > So:
 > 1] How can I write non-member functions that can use a template parameterized
 >    by a constant-expression, without resorting to the friend-trick shown above?

You can't. It's a botch.

I introduced the restriction against non-type template arguments for
template functions when I wrote the ARM to avoid promising something
I couldn't keep, and indeed exactly stating which non-type template
arguments can be deduced isn't trivial.

 > 2] If I can't, shouldn't the standard be modified?

It has been. Your examples are now legal as written.

 > 3] Is the friend-trick legal?

That too.

 - Bjarne