Topic: template< class T, size_t S > problem


Author: herbs@interlog.com (Herb Sutter)
Date: 1995/07/17
Raw View
In article <3uc9bf$4l5@hrz-ws11.hrz.uni-kassel.de>,
   renes@nick-0.neuro.informatik.uni-kassel.de (Rene Seeber) wrote:
[edited for brevity]
>
>template< class T, size_t S >
>class FixedArray {};
>
>template< class T, size_t S >
>ostream& operator<<( ostream& os, const FixedArray<T,S>& fa ) {}

Check out D&E section 15.6.1.  This used to be illegal "because non-type
arguments couldn't be deduced"; that is, you couldn't have a function template
with a non-type argument, which S is above.

This previously illegal style has since been accepted, but compilers are still
catching up.  For example, BC++ 4.5 gives the following diagnostic:

 Template functions may only have 'type-arguments'

 A function template was declared with a non-type argument.
 This is not allowed with a template function, as there is no way to
 specify the value when calling it.

And so of course it then prints a second diagnostic like the one you
mentioned, complaining that S wasn't used (since it wasn't).

Suggestion for a workaround in the meantime: Instead of using a second
template parm to enter the size, you could make it a ctor parm.

 template< class T >
 class FixedArray { public: FixedArray(size_t size); };

 template< class T >
 ostream& operator<<( ostream& os, const FixedArray<T>& fa ) {}

This is legal under both the old and the revised rules; and it's another way
of stating that a FixedArray, once created, knows its size and can stream
itself.  When the compilers catch up, you can go back to the above style if
you feel you need to for reasons not shown above.

Hope this helps (personally I'm waiting longingly for parm defaults),

Herb


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Herb Sutter                 2228 Urwin, Ste 102         voice (416) 618-0184
Connected Object Solutions  Oakville ON Canada L6L 2T2    fax (905) 847-6019