Topic: refs to arrays as template args


Author: Stefan Schwarzer <sts@ica1.uni-stuttgart.de>
Date: 1999/01/30
Raw View
I recently encountered some unexpected behavior when I tried to
compile the following piece of code:

template<class T>
void f(T &){}

int main(){
  int a[]={1};
  f(a);
}

I was expecting that the compiler would deduce T=int [1], instantiate
and compile. However, it seems that by the template argument deduction
procedure (in 14.8.2.1) it is required to deduce T=int *
But then, given the function signature

void f(int *&){}

the compilation will fail (and does), because the array cannot be bound to
a reference to a non-const pointer. I am wondering why the standard
requires the deduction as T=int * and not something like
T=int * const  or even better the exact type -- what is the rationale
behind that?

If I want a template function like the above to work for arrays as well
as classes with overloaded (possibly non-const or virtual) operator[]
how would I do that?

Thanks for enlightenment!


--
Stefan Schwarzer                office:  +49-(0)711-685-7606   fax: x-3658
Uni Stuttgart, ICA 1            e-mail:          sts@ica1.uni-stuttgart.de
Pfaffenwaldring 27              pgp public key available on request/finger
70569 Stuttgart, Germany             http://www.ica1.uni-stuttgart.de/~sts
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]