Iterators and ints get confused in template parameter
I'm writing an STL-style data container, and this problem is puzzling
me. The following code should demonstrate the problem:
//----------------------------------------------------------
#include <iostream>
#include <list>
template<typename T>
class MyClass
{
public:
void assign(size_t amount, const T& value)
{
std::cout << amount << " " << value << std::endl;
}
template<typename InputIterator>
void assign(InputIterator first, InputIterator last)
{
while(first != last)
{
std::cout << *first << std::endl;
++first;
}
}
};
int main()
{
std::list<int> l1, l2;
l1.assign(10, 5); // ok
l2.assign(l1.begin(), l1.end()); // ok
MyClass<int> mc;
mc.assign(l1.begin(), l1.end()); // ok
mc.assign(10, 5); // error, wrong function gets called
}
//----------------------------------------------------------
I'm using gcc 4.1.2.
I understand *why* the problem is happening. However, I don't
understand why it's not happening with std::list, and I have no idea how
to get around the problem (in the same way as std::list does).
I have looked at the std::list source code in the gcc C++ libraries,
and I don't see any fancy trick being used to differentiate between the
two assign() functions. Yet it just works with std::list, but it doesn't
work with my code.
Any pointers?