>Because thats what the instructions say to do
Clearly the author of your instructions isn't familiar with the C++ standard and didn't consider what would happen when T is int. Put simply, in overloaded name resolution, cv and reference qualifiers are ignored. So both insert functions are of type
Code:
void RubberArray<int>::insert ( int, int );
As far as the compiler is concerned. There's basically no way to get around it cleanly without something hackish like your function pointer solution, or something error prone and reliant on the class internals like this:
Code:
template <typename T, typename I = int>
class RubberArray
{
I indexoffset_;
public:
void insert ( const T&, I i);
inline void insert ( I i, const T& a) { insert(a, i); }
inline void insert ( const T& v) { insert(v, indexoffset_ ); }
};
int main()
{
RubberArray<int> ra0; // Error, T and I == int is ambiguous
RubberArray<int, unsigned int> ra1; // Okay, T and I are now different
RubberArray<double> ra2; // Okay, T and I are different
ra0.insert ( 10 );
ra1.insert ( 10 );
ra2.insert ( 10.0 );
}
A third alternative is to add a level of abstraction. Define RubberArray to take a templated object rather than any type T (ie. RubberArray is not a template class as you had it). Though this adds a level of complexity that would be prohibitive, so you really don't have any good options thanks to somebody's poor design.