arrays decaying to pointers
Hello,
arrays decay to pointers, which allows for:
template<class Iter>
void f(Iter first, Iter theend);
int tab[]={1,2,3};
f(tab,tab+3);
and Iter is deduced as int*.
However, if I go through a forwarding function:
template<class A,class B>
void g(A const&a, B const&b){ f(a,b); }
g(tab,tab+3);
f now receives a reference to an array of _const_ int and a reference
to a const pointer to int. But just passing f a pointer to const
int and a pointer to int is already an error as the compiler can't
deduce Iter anymore (yes, one could dream about a compiler clever
enough to deduce Iter as const int* in this case).
So it looks like my best bet is to define f with 2 different template
parameters:
template<class Iter1,class Iter2>
void f(Iter1 first, Iter2 theend);
and possibly add various checks to make sure Iter1 and Iter2 are
compatible. Is there some better way?
Note that with C++0X, perfect forwarding works (but you really need to
make sure there is no imperfect forwarding along the way), or you can
consider that the standard way to pass the array is begin(tab),
end(tab) and forget about the whole array mess.
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]