Re: Iterators and ints get confused in template parameter
Kai-Uwe Bux wrote:
Juha Nieminen wrote:
Could perhaps <tr1/type_traits> be used for this purpose?
Yes.
This seems to work. Does anyone have any objections (besides it
using tr1)?
//---------------------------------------------------------------------------
#include <iostream>
#include <list>
#include <tr1/type_traits>
template<typename T>
class MyClass
{
template<typename ValueType>
void dispatchAssign(ValueType amount, ValueType value,
std::tr1::true_type)
{
std::cout << amount << " " << value << std::endl;
}
template<typename InputIterator>
void dispatchAssign(InputIterator first, InputIterator last,
std::tr1::false_type)
{
while(first != last)
{
std::cout << *first << std::endl;
++first;
}
}
public:
void assign(size_t amount, const T& value)
{
dispatchAssign(amount, value, std::tr1::true_type());
}
template<typename InputIterator>
void assign(InputIterator first, InputIterator last)
{
dispatchAssign(first, last,
std::tr1::is_integral<InputIterator>());
}
};
int main()
{
std::list<int> l1, l2;
l1.assign(10, 5);
l2.assign(l1.begin(), l1.end());
MyClass<int> mc;
mc.assign(l1.begin(), l1.end());
mc.assign(10, 5);
}
//---------------------------------------------------------------------------