Re: arrays decaying to pointers
On 2011-05-30 14:56, Marc wrote:
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?
I would stay away from adding several overloads. Instead I would follow the spirit of make_tuple as described in TR1 (modulo reference_wrapper unwrapping but plus decaying): The function template takes arguments by const& Ti, but would convert them to std::decay<Ti>::type. This requires that you can create a helper function like
template <class T>
typename decay<T>::type decay_copy(const T& v)
{ return v; }
with C++03 means. IMO this should not be too hard to realize (Implementing the decay trait should be possible with C++03 means). The advantage over using begin/end is that this decay-forwarder can be used for other decayable types. The final call syntax would then be:
void g(A const&a, B const&b){ f(decay_copy(a), decay_copy(b)); }
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]